Forum Moderators: open

Message Too Old, No Replies

refresh a div

rotating adsense ads with javascript

         

majjk

10:31 pm on Jun 12, 2006 (gmt 0)

10+ Year Member



I would like to rotate adsense ads without refreshing the entire page. The adsense stuff is delivered by phpadsnew, so I need to refresh the following div with all its content:

<div class="banner">
<script language='JavaScript' type='text/javascript'>
<!--
if (!document.phpAds_used) document.phpAds_used = ',';
phpAds_random = new String (Math.random()); phpAds_random = phpAds_random.substring(2,11);
document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
document.write ("http://www.mydomain.com/asf/stats/adjs.php?n=" + phpAds_random);
document.write ("&amp;what=zone:12&amp;target=_blank");
document.write ("&amp;exclude=" + document.phpAds_used);
if (document.referer)
document.write ("&amp;referer=" + escape(document.referer));
document.write ("'><" + "/script>");
//-->
</script><noscript><a href='http://www.mydomain.com/asf/stats/adclick.php?n=a160758b' target='_blank'><img src='http://www.mydomain.com/asf/stats/adview.php?what=zone:12&amp;n=a160758b' border='0' alt=''></a></noscript>
</div>

I'm really rubbish at js so I'm having a bit of trouble here, but I'm sure it's a piece of cake for you javascript people out there. Any ideas how to do this?

jshanman

3:53 pm on Jun 13, 2006 (gmt 0)

10+ Year Member



HTML: <div id="banner"><a href='http://www.mydomain.com/asf/stats/adclick.php?n=a160758b' target='_blank'><img src='http://www.mydomain.com/asf/stats/adview.php?what=zone:12&amp;n=a160758b' border='0' alt=''></a></div>

JS:

function rotateAds() {
if (!document.phpAds_used) document.phpAds_used = ',';
phpAds_random = new String (Math.random()); phpAds_random = phpAds_random.substring(2,11);
var r = (document.referer)? "&amp;referer="+escape(document.referer) : "";

var b = document.getElementById("banner");
b.innerHTML = "";
var js = document.createElement("script");
js.type = "text/javascript";
js.src = "http://www.mydomain.com/asf/stats/adjs.php?n="+phpAds_random+"&amp;what=zone:12&amp;target=_blank&amp;exclude=" +document.phpAds_used+r;
b.appendChild(js);
}

window.setTimeout("rotateAds()",30000);

This writes the no script in the div, which if there is no script, then it is never replaced. However, if script is running, then the function clears the previous contents of the div, then creates a new js link to create a new ad. The settimeout repeats this every 30 seconds...

- JS