Making AdSense work with XHTML
Why won't AdSense work with true XHTML?
iframe in which to dynamically serve advertisments. This is fine if the page is going to be regular HTML or non-strict versions of XHTML being served as
iframe as expected, and everything works fine and dandy. Unfortunately, for webmasters serving XHTML pages as
application/xhtml+xml, the Google method doesn't work.
Document.Write() will not work in correctly served XML pages that are handled by an XML parser. Ian Hickson provides an explanation of why this is the case. As we have already observed,
Document.Write() is used to generate the
iframe; therefore, the advertisements never appear.
The second problem is the
iframe would make the page invalid XHTML.
How can I make it work?
One way to get around these problems is to simply serve AdSense code as
text/html. In order to do that, it will be necessary to create a separate web page with a MIME type of
text/html and then insert it into the desired page as an
object. Here is what the separate document should look like:
For those webmasters using the content negotiation script to serve either
text/html, it is possible to make use of the existing code to determine which of the two methods to employ. In the original content negotiation script, the MIME type is held in a variable called
$mime. If the value of that variable is "application/xhtml+xml", the
This solution has been tested with Internet Explorer 6.0, Firefox 0.92, and Opera 7.0.