AdSense for XHTML

Posted Aug 21, 2004 in JavaScript, Making Ends Meet, si-blog, XHTML.

In a previous entry, I wrote about how it was impossible to get Google's AdSense to work in a document served as application/xhtml+xml, because Document.Write() does not work in XML, and Google uses it to create an iframe for the ads.

After giving it a little more thought, I have come up with a solution. It is by no means elegant, because I have had to cheat a little bit. I create a separate text/html document that contains the AdSense code, and then import that as an object into the application/xhtml+xml. I then use CSS to hide the object from Internet Explorer. Since IE is being served text/html anyway, it gets the regular AdSense iframe that the application/xhtml+xml cannot see.

Okay, that was poorly described, wasn't it? I promise I will do a proper article on it in the very near future. For now, you can see it working on my archive page.


  1. Gravatar

    Clever trick! I can see a couple of problems in my browsers though: the ads show up twice in Safari, and not at all in Firefox. Opera shows them once.
    How about using the content negotiation script to send the object only to browsers that get application/xhtml+xml? That would remove the need for CSS tricks to hide a set of ads.

    Posted by Roger on Aug 22, 2004.

  2. Gravatar

    Hmmm. They show up in Firefox for me. I'm sure with some refinement, the we can probably get it to work the way we want.

    Posted by Simon Jessey on Aug 22, 2004.