More AdSense for XHTML

Posted Aug 22, 2004 in Making Ends Meet, PHP, XHTML.

I have made a substantial revision to the article I wrote about making AdSense work with documents served as application/xhtml+xml. I have managed to remove the need for special CSS, and the new solution is more elegant than before.

The revision came out of something Roger Johansson had said in my comments. He suggested sniffing the Accept header and then using PHP to serve either the object, or the normal AdSense JavaScript. Since I am already doing some sniffing, the information I need is already contained in a variable. A simple if statement that tests that variable allows me send the correct code for the type.

Anyone who finds the technique useful should go to my archive page and visit my sponsors!


  1. Gravatar

    I see you've also integrated it into the rest of your site. And the article is very useful. Many happy developers now, I think.

    Posted by Rob Mientjes on Aug 23, 2004.

  2. Gravatar

    Yeah, I thought I might as well put my money (or YOUR money) where my mouth is and begin serving the ads on more of the pages. Advertising is, and always will be, vulgar as far as I am concerned, but money doesn't grow on trees and AdSense seems relatively harmless.

    Posted by Simon Jessey on Aug 23, 2004.

  3. Gravatar

    Is it just me? I do not see anything related to Google on your Archive page. Using Firefox, _only_.

    Posted by David on Aug 23, 2004.

  4. Gravatar

    Well I'm using Firefox 0.9.2, and I see the Google ads (in the right-hand sidebar) just fine. Is it just the archive page that you cannot see the ads on, or can you not see them at all?

    So far, I've tested in: IE6, IE5.5, IE5, Firefox 0.9.2, and Opera 7.0 (all on Windows XP and Windows 2000). Danvine's iCapture is down so I cannot check Safari at the moment.

    Posted by Simon Jessey on Aug 23, 2004.

  5. Gravatar

    I just looked at another Windows 2000 machine, and there are no ads when served as application/xhtml+xml. For some reason, some browsers are not seeing them, and it may be operating system related. It can only be related to the use of <object>, but I am not sure what the... er... objection is.

    Posted by Simon Jessey on Aug 23, 2004.

  6. Gravatar

    I think I fixed it - I forgot to give the object a MIME type.

    Posted by Simon Jessey on Aug 23, 2004.

  7. Gravatar

    It is some very clever code, Simon, but for one minor problem. It is strictly forbidden in the Adsense terms and conditions to alter the Javascript used to call the ads.


    <quote>2. Implementation and Operation of Ads and Search Results. You agree to comply with the specifications provided by Google ... including without limitation by not modifying the JavaScript or other programming provided to You by Google in any way, unless expressly authorized in writing by Google</quote>

    The trouble is, if you implement this system and you get banned because of it (Google have cancelled accounts for much less than this), then you are not really doing yourself or your clients much of a favour. If I were you, I'd add a specific disclaimer on your article about this.

    Smart-looking site, by the way! ;)

    Posted by RichardPW on Aug 28, 2004.