Recent Article links:


AdSense and AJAX: Not a happy combination

After releasing my AjaxBerlee WordPress theme George and Milan’Che reported problems with Google’s AdSense. This left me with a problem. Because new page requests in my theme are parsed over the old ones.

AdSense works as follows. The Mediapartners-Google spider indexes the site, when visitor now requests one of your pages it will load the external JavaScript from the Google domain. In that JavaScript the text advertise is displayed.

So far so good. An AJAXified page will initially display the AdSense advertise very well (because the pages are backwards compatible). The visitor clicks on a link and now only a part of the page get reloaded, but not the AdSense advertise. Because it’s possible to load an JavaScript into JavaScript, but its not possible to reload JavaScript into JavaScript (for scripts that aren’t build for it).

I suspected that Google had some kind of a refresh function build in where I could specify which URL the currently displayed AJAXified site was. So when you click on an AJAXified link, the Ajax JavaScript calls a Google refresh function. (this is actually how the Google Analytics support works in the AJAX JavaScript)

I was surprised to find out that there was not such a function in the AdSense JavaScript. So I tried to find out how AJAXified sites like Google Maps and GMail works with AdSense. Well, actually, they don’t!

GMail has some uses some AJAX function to display realtime new incoming messages, but the actually displaying of mail goes thru frames and plain old loading of pages! And Google Maps actually doesn’t use AdSense at all… Well, they do, but its parsed into the page and doesn’t use external JavaScript at all, as described above.

I found one workaround by Kevin Cho who used an iframe. In that iframe the AdSense got loaded with the keywords of that page specially dedicated for AdSense for that site. You need to type these manually, which is a hell of a job if you have a lot of pages. And it’s probably against the Program Policies which are very clear about cloaking.

I decided not to implement that workaround because of the Program Policies violation. But I will publish a modified ajax.js that is compatible with AdSense but does not have the enhanced page loading.

Comments (15 comments)

Hello, Nico. Thanks for your research into updating Google Adsense ads in AJAX apps. Have you noted any developments in this area, since you wrote this article? Obviously, the best solution would be if Google were to extend their API to allow for refreshes of the Adsense content.

Eric Nguyen / May 14th, 2007, 9:22 pm

Here is the solution we came up with for the problem…

Basically, AdSense writes out an IFrame behind the scenes in it’s ad script. We can grab the a div that contains the script and look for an IFrame within it. Then you can dynamically switch the src of that IFrame.

Here is a quick sample html file (just paste your AdSense script in the div)…


Yes, the url will keep growing so it might be better to add and then replace a known parameter, but this is just for demonstration purposes.

Leon / August 15th, 2007, 6:15 pm

This was great. Thanks Leon

Reza / January 4th, 2008, 7:50 am

Glad it could help!

Leon / January 9th, 2008, 2:59 pm

Is it in compliance with Google TOS?

Yasir / April 9th, 2008, 5:36 am

Great posting. I’m currently researching the issue as well as I’m confuser how Google has GMAIL refreshing and we as publishers are unable to refresh our ads.


nobosh.com / April 23rd, 2008, 4:26 am

I have successfully done this technique on my site using IFRAME. However in my case stuff that was in IFRAME had to be refreshed anyway, so I didnt have to hide any keywords.

However there is one solution, without breaking the TOS (I think)… when you change the source of the file just make the iframe src filename related to the topic you want ads to be displayed for. It worked for me. check http://www.hungrysiam.com

www.hungrysiam.com / July 9th, 2008, 6:27 am

By the way I wrote a little article on shvoong.com about this topic.


it might help a bit.

by the way if you visit my site then keep in mind that only adsense unit in the google maps marker has this IFRAME set up. the one bellow the map doesnt use this technique.

www.hungrysiam.com / July 9th, 2008, 2:58 pm

According to Google’s terms and conditions, modifications to the JavaScript or placing ads of pages which do not contain content are verboten. So how does this apply to dynamically inserting ads?

iGuide / October 1st, 2008, 7:53 am

I agree. I think placing ads in their own iframe just sounds plain wrong. Unless Google specifically says its ok, I wouldn’t do it.

Map Wiki / October 20th, 2008, 1:22 am

How about using the iFrame idea, using Google’s standard script, but make the page within the iframe a server side page that accepts a URLString of the page id or similar. Then make that page grab the keywords, description and any other useful content such as the tags?

Would only need to be a simple page as nothing other than Google would accept it. You could even go one step further and use the real natural url. For example:

real URL: test.com/page/content-title
Ad URL: test.com/ad/page/content-title
Ad URL: test.com/page/content-title?ad=true (the rendering of the site would need to grab the last variable and know it’s acutally an ad page).

Anyone tried this approach?


Gregory Brine / January 21st, 2009, 1:36 am

try, I will answer after. And the author thanks!

Ivan / February 8th, 2009, 3:00 pm

There is nothing wrong with dynamically refreshing the src URL of Google’s ad IFRAME without a complete page refresh. The Adsense Code is not being touched and it does not violate the Google TOS.

Leon / March 5th, 2009, 2:49 pm

I spent so much time messing around with solutions, I ended up creating one of my own.

You can find it over at my blog :)

Shawn K. / January 24th, 2010, 7:42 am

I find a very easy solution (after hours of squezzing my brain) to keep adsense with ajax requests with prototype

var dom=$(‘id of adsense div’).clone(true)
let you keep a element clone of current adsense ad where you can replace anywhere on your DOM, after fex doing an ajax request

$(‘id of adsense div’).cleanWhitespace().update(dom);


Jordi Marti / March 31st, 2010, 6:59 pm

What do you think?

Comments are closed for this post.

ACF loading animated gif