AdSense for XHTML article
Posted Aug 21, 2004 in CSS, Making Ends Meet, XHTML.
I have put together an article over at Keystone Websites that explains the process better than my pathetic blog entry about it. It has been done rather hastily, so it may get revised a wee bit over the next few days. I hope you find it useful.


Comments
Simon, isn't it the case that commented out scripts will not be interpreted? There is no reason to keep them (comments) even in html documents. Also, if you use xhtml, why is the type text/javascript?
Posted by Moose on Aug 21, 2004.
I'm not sure about the comments thing - I'll check that. It works, so I guess the JavaScript is being interpreted. The JavaScript type is correct - the whole point of using the object is that it allows us to use HTML as text/html.
Posted by Simon Jessey on Aug 21, 2004.
If you use comments, they will be treated as such. Which basically means that your script won't be read by the browser. The correct MIME type for Javascript is 'application/x-javascript' by the way. Nobody ever registered one and this is one the server is using...
Posted by Anne on Aug 22, 2004.
The comments were removed yesterday, Anne. And I'm not sure you are right about the JavaScript. Remember that we actually DON'T want the JavaScript to work on the XHTML page, only when it is transformed into HTML by the content negotiation. The JavaScript is NEVER served as application/xhtml+xml, so shouldn't it remain text/javascript?
Posted by Simon Jessey on Aug 22, 2004.
How about using this where you want to insert the ads:
<?php
if (stristr($_SERVER[HTTP_ACCEPT], "application/xhtml+xml")) // or whatever you use to check how to serve the document
echo("<object data="/includes/google.html"></object>
"); // path to where your HTML document containing the Adsense code is
else {
echo() // several lines that print the "standard" Adsense JavaScript
}
?>
I gave it a try and it seems to work.
So this could be a solution, unless the adsense code somehow gets confused by being wrapped in an object _and_ in an iframe (could it be? no idea.), or Google disapproves of this technique.
Posted by Roger on Aug 22, 2004.
Another way of doing it would be to set a boolean variable during the content negotiation process, and then use that variable to decide whether or not to serve the script or the object. That would be pretty elegant, I think.
Posted by Simon Jessey on Aug 22, 2004.
I said 'application/x-javascript', not 'application/xhtml+xml'. The thing is that 'text/javascript' has never been registered. See also: http://annevankesteren.nl/archives/2003/08/the-mime-type-of-javascript for more information.
Posted by Anne on Aug 22, 2004.
So you are saying that there aren't ANY circumstances where text/javascript is correct, because it was never registered? I am surprised that this hasn't been properly addressed, especially with the existence of ECMA Script.
Posted by Simon Jessey on Aug 23, 2004.
Incidentally, I tried changing the MIME type to "application/x-javascript". It immediately broke on several browsers, including Opera (which surprised me). I have no choice but to stay with "text/javascript" to make it work.
Posted by Simon Jessey on Aug 23, 2004.
I guess I should have told you that ;-). But indeed, 'text/javascript' is incorrect.
Posted by Anne on Aug 24, 2004.