Jump to content

Creating a rotating banner for Asynchronus JS tag


netvillage

Recommended Posts

It seemed to be a popular question on here but no solution, so I came up with something that seems to work pretty good. It uses a few jquery lines and doesn't use iframes or force a page refresh. Enjoy!

<div id="myad"><ins data-revive-zoneid="2" data-revive-id="123456789"></ins></div>
<div id="adScript"><script async src="http://xxxxx/www/delivery/asyncjs.php"></script></div>

<button type="button" onclick="rotateAd('myad')">Rotate Ad</button>
<script>
function rotateAd(addiv) {
// Remove old ad
 $( "#"+addiv ).empty();
 $( "#adScript" ).empty();
 window.reviveAsync={};
 
 // Create new ad
 //   <ins> element
 var ins = document.createElement('ins');
 ins.setAttribute('data-revive-zoneid', '2');
 ins.setAttribute('data-revive-id', 'de77d907e60f2f4544b2c1842c4e97ea');
 document.getElementById(addiv).appendChild(ins);
 
 //   <script> element
 var s = document.createElement("script");
 s.type = "text/javascript";
 s.src = "xxxxxx/www/delivery/asyncjs.php";

 s.innerHTML = null;
 document.getElementById("adScript").innerHTML = "";
 document.getElementById("adScript").appendChild(s); 
}
</script>
 

Just noticed I should prob make a parm out of the zone id

 

Oh I just used a button to test. The rotateAd should be on a timer of some sort

Link to comment
Share on other sites

Actually Here's the same improved a little bit. didn't need the separate DIV for script

<div id="myad"></div>

<button type="button" onclick="rotateAd('myad',2)">Rotate Ad</button>
<script>
function rotateAd(addiv,zone) {
  // Remove old ad
 $( "#"+addiv ).empty();
 window.reviveAsync={};
 
 // Create new ad
 //   <ins> element
 var ins = document.createElement('ins');
 ins.setAttribute('data-revive-zoneid', zone);
 ins.setAttribute('data-revive-id', 123456789');
 document.getElementById(addiv).appendChild(ins);
 
 //   <script> element
 var s = document.createElement("script");
 s.type = "text/javascript";
 s.src = "//xxxxr/www/delivery/asyncjs.php";
 s.innerHTML = null;
 document.getElementById(addiv).appendChild(s); 
}
</script>
 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...