Wednesday, August 9th, 2006

How to Run Ads with Ajax

Category: Advertising

Anyone who’s running an Ajax site knows the frustration of showing static ads on a page that might not refresh for minutes or hours. Until the ad providers begin offering rotating Ajax ads (which would be suit some non-Ajax sites as well), you’re stuck with the same ad for a long time. Or are you?

In this JGuru article, Kevin Cho outlines a hack to offer rotating ads. Summary: Put the ad in an IFrame, embed the IFrame on your page refresh the IFrame on every XHR call. Sprinkle invisible keywords into the IFrame for extra context.

refreshAd() is where the magic happens…


  1. var oXmlHttp = createXMLHttp();
  3. oXmlHttp.onreadystatechange = function() {
  4.      if (oXmlHttp.readyState == 4) {
  5.           var storyDiv = document.getElementById(layerName);
  6.           storyDiv.innerHTML = oXmlHttp.responseText;
  8.           refreshAd();
  9.       }
  10. };
  12.  function refreshAd() {
  13.       var currentTime = new Date();
  14.       googlead.location = "pages/ad.jsp?s=" + currentTime.getTime();
  15. }

If you’re wondering if this is fair game by Google, you’re not alone. Does it violate Google’s adsense policy? Maybe, but that’s not to say Kevin hasn’t considered the implications.

Ok~ so let see what is fair use of this adsense. In my opinion (yours may differ..even Google), if there’s a content change in your web site then it’s fair to show a new ads on your page … That’s right! I’m using IFRAME.. I can already see you’re not surprised as if you already know the solution. There’s really no other option since any other solutions will be breaking Google’s TOS.

Check out Kevin’s article for all the details.

Posted by Michael Mahemoff at 12:44 pm

3.4 rating from 60 votes


Comments feed TrackBack URI

Thanks alot, this is very helpfull. Iv been trying something simular in PHP but i might aswell scrap it and use this method.

Comment by Jason Bates — August 28, 2006

We have been doing Ajax-based Advertising Widget for a while/ We are the creater of Gizmoojo Adgent Widget. It’s an Amazon Associates Adveristing widget that rotates the product ads without the use of flash. This is now the highest subscribed widget for money makers at

Comment by khang toh — October 4, 2006

The IFRAME adds some complexity when trying to add back-button functionality to your ajax app.

Comment by Mark Maunder — October 8, 2006

This technique will generate a lot of money for ajaxified chat applications.

Comment by Jordan — May 21, 2007

Sorry, the HTML didn’t go though in the post.

You can get the sample file at…

Comment by Leon — August 15, 2007

I tried this and just wasn’t satisfied, I spent a couple hours looking around and making my own version. Feel free to check it out, it fixes issues that many other functions create.

Adsense and AJAX: A Solution

Comment by ShawnK — January 23, 2010

After switching to iFrames for ads and using Ajax to power our photo galleries we saw much better engagement with our visitors. Pages per visit skyrocketed! I noticed though that the number of ads served didnt match up to the pageviews. I started to think that visitors were clicking through each page so quickly that the networks didn’t get the chance to load their ad would pass back to the default. The problem is everyone was passing back. So even though the user experience is much better, we’ve been forced to remove frames and go back to refreshing the entire page with every click. Pageviews have dropped significantly, load time has increased significantly, but ad revenue is up. Has anyone else encountered this problem?

Comment by FrancoEX — August 25, 2011

Leave a comment

You must be logged in to post a comment.