Tuesday, November 13, 2007

AdSense on AJAX Pages

MovieLandmarks is a 100% AJAX page. This makes it a really nice site to explore for people, but not so friendly for the Google AdSense system. I'm experimenting with AdSense ads over on the right side of the map, hopefully with the addition of the directory it will start showing some relevant ads in a day or so. I have it setup to refresh the ad when the movie selection changes, but I don't know if it can pick up the change to the location using hashes or not.

I'd really like it if I could 'feed' the adsense system the dynamic content from the page, but apparently it doesn't work like that.

I don't like the idea of having ads for fireplaces or sonic software being shown all the time, so if it doesn't improve in a few days I'll probably pull it off.

Making it refresh is pretty easy. Create a div for your adsense ad, put your adsense code into it and then call this function to refresh it:

/* Refresh the Google Ad iframe
*/
function refreshAd( content ) {
var d=document.getElementById('adsense');
if(d){
var s=d.getElementsByTagName('iframe');
if(s && s.length){
s[0].src=s[0].src+'&'+new Date().getTime();
}
}
return true;
}
The content argument is there so I could experiment with feeding it content (which doesn't work).

3 comments:

Roger Wu said...

did you ever solve this problem with that refresh() function?

Anonymous said...

The url will be longer and longer over time. Use this code instead:

var src = (s[0].src.split(/&xtime=/))[0];
s[0].src = src + '&xtime='+new Date().getTime();

MovieLandmarks said...

Thanks, I discovered that as I was trying to get things working.

No, I never solved it and finally have in and made a more 'static' page.