Adsense and AJAX: A Solution

Tired of fight with Adsense when using AJAX? That's exactly how I've been the last couple days. For such a widely spread service, it's terrible how bad flexibility for Web applications can be!
Anyways, most reputable programmers wont be relying much on iFrame, so here's an easy solution - just run this function every time you want to update the ads on your page.
I must warn you, DO NOT abuse this, Google can and will ban your account if impressions are being forced. I can't vouch that Google will even approve of this method, but from reading the ToS it should be acceptable giver the [end] user sent a request to change page content.
var redirects = 0;
function updateAdsense() {
redirects++;
var ads = document.getElementsByTagName('iframe');
var src;
if(ads && ads.length){
for (var i = 0; i < ads.length; i++){
src = ads[i].src.split('&');
if(redirects > 1)
src.pop();
src = src.join('&');
src = src + '&'+new Date().getTime();
ads[i].contentWindow.location.replace(src);
}
}
}
You can see I use "location.replace", this solves issues with Adsense updates effecting browser navigation. In basic, it makes the back button work correctly.
As you can see this code is rather lazy, I will be providing an update soon.
Update
var redirects = 0;
function updateAdsense() {
redirects++;
var ads = document.getElementsByTagName('iframe');
var src;
if(ads && ads.length){
for (var i = 0; i < ads.length && ads[i].id.substr(0,16)=='google_ads_frame'; i++){
src = ads[i].src;
if(redirects > 1) {
src = src.split('&');
src.pop();
src = src.join('&');
}
src = src + '&'+new Date().getTime();
ads[i].contentWindow.location.replace(src);
}
}
}


January 24th, 2010 - 06:42
I always appreciated Ajax and never used in my site because I was using Adsense as my primary source of revenue. I could not sacrifice the possibility of earning a few dollars over improving user experience. I believe and I’m waiting for some genius people like you will find some solution and make our life easier.
January 28th, 2010 - 18:32
This is great – although I was a little leary of it when I saw it was being used to update the ads. But you have a valid point – the user is clicking through your site in order to load new content, it just isn’t registering with Adsense and thereby not getting new content to the ads. I think that this is a legitimate use.
Has anyone asked Google?
Don Gilbert´s last blog ..Why Your Small Business Can’t Ignore Social Media Any Longer
January 28th, 2010 - 21:58
From what I have heard, Google does advise against it, but I have yet to hear them actually say no.
This method does seem to comply with the ToS as it does not modify their code nor cause unintended “clicks”.
March 2nd, 2010 - 19:51
I’m using something similar to this code. I’ve read through the AdSense TOS, and from what I can tell it should be kosher, as you say. It’s certainly in the spirit of ad serving, in that you issue a reload – essentially in place of the browser doing it – in response to a user request for new page content. We refresh virtually everything on the page, so all’s well there.
The big limitation I’ve found is that when using banner ads, a portion of those are Flash – which doesn’t use an iframe. So, there’s nothing to target for the refresh, and it’s a complete roadblock from that point until a hard page reload occurs.