1

I have an ad script running, which is hardcoded into my HTML. Now I want to refresh that ad tag after 240 seconds. Easy way would be to simply load the ad tag in an iframe, and put a refresh on that, but iframes cannot be used in my case.

Right now the ad tag loads like this:

    <div class="banner_728">
        <script type="text/javascript"><!--
            bb = new Object();
            bb.size = "728x90";
        //--></script>
        <script type="text/javascript" src="//adserver.com/tags/tags.js"></script>
    </div>

How could I re-execute that javascript after a set amount of seconds, whilst leaving that ad-tag hardcoded into my site.

3
  • what do you mean by refreshing the "add tag"? Commented Jul 18, 2017 at 16:10
  • basically the ad gets executed again after 240 seconds Commented Jul 18, 2017 at 16:14
  • Possible duplicate? stackoverflow.com/questions/4738595/… Commented Jul 18, 2017 at 16:15

1 Answer 1

1

Apparently You'll need to load the Ad Server JS again.

var bb, created=false;
function reRun() { 
    bb = new Object();
    bb.size = "728x90";

    if(created) document.body.removeChild( document.getElementById("id-ads") );

    // try this instead
    var js = document.createElement("script");
    js.type = "text/javascript";
    js.src = "//adserver.com/tags/tags.js";
    js.id = "id-ads";
    document.body.appendChild(js);

    created = true;

}
var TIMEOUT = setTimeout( reRun , 240 * 1000 );
reRun();

You may or may not have security issues loading external js on the fly. Please read this post which is going to be helpful for your needs:

How do you dynamically load a javascript file from different domain?

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much!! I think we are nearly there. I am getting an error though. "Uncaught TypeError: Illegal constructor at reRun". Points to the 6th line in your example. The one starting with "var script".
Sorry, this is using PrototypeJS. Download here: prototypejs.org/download or if you want plain js I've edited the answer, please test that ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.