Setting up AdSense on an XHTML based site

Yesterday, I decided to sign up for Google AdSense. The reason? Well, to be honest, I just wanted to have a bit of a poke at it, and see how it worked and what you could do with it. The sign-up process was nice and easy, and I would have been able to start poking around with it within minutes but I needed to go to bed.

When I got up, I quickly followed the online instructions, and added the resultant piece of code to my site template. It worked… sort of. In Internet Explorer, the nice little google ad was displaying away, bright as a button. In Firefox, on the other hand, there was nothing - the advert refused to show. It turns out that the reason for the problem was that the JavaScript used by google uses document.write() statements. As Ian Hickson explains, document.write cannot be used in XML documents, which is what an XHTML document is. Thankfully, there’s a fairly easy solution, which lets AdSense work with XHTML documents. Basically, instead of simply inserting google’s script into your page, you put that code into a normal HTML page, which you then include into your XHTML page using an <object> tag. It works nicely, and adverts generated in this way are still targeted to the actual XHTML page rather than the contained HTML page.

As for money earned from the advertising, my policy is this. First and foremost, it will be used to help cover any costs incurred in the production of (web hosting, that sort of thing). Any money raised via advertising over and above that will be given to a charity (or charities) of my choice.

permalink | View blog reactions


TrackBack URL for this entry:

other relevant pages

about wwm is a resource for web developers created by Neil Crosby, a web developer who lives and works in London, England. More about the site.

Neil Crosby now blogs at The Code Train and also runs, The Ten Word Review and Everything is Rubbish.