Javascript on XHTML pages

There are lots of rumours about Javascript in XHTML served as different MIME types. I'll try to explain what's really going on. Shortly, innerHTML works everywhere (even for XHTML served as text/xml); document.* collections work identically for application/xhtml+xml and text/html.

XHTML served as text/html

There are two main differences in DOM between valid HTML and XHTML served as text/html - in XHTML all elements are in lowercase and UAs don't create implicit elements such as tbody in XHTML. That's all! You don't need to put inline styles and scripts in CDATA sections. UAs handle these XHTML pages practically the same as they handle HTML ones, xml well-formedness is not checked. And of course, all document.* collections work, innerHTML works either. And yes, document.all works even in Firefox (since version 1.5).

Yes, there's also a problem of marking inline scripts and style as CDATA sections, but with well-coded unobtrusive javascript there's no need for inline scripts, and there's definately no need for inline styles except rare cases (even LJ allows using external CSS files).

XHTML served as application/xhtml+xml

That is the preferred MIME-type for XHTML pages. Commonly it is used with content-negotiation mechanism to serve pages as application/xhtml+xml for browsers that support it and to serve pages as text/html for browser that doesn't support application/xhtml+xml. Some people say that most of neat DOM methods and properties do not work in XHTML document served as application/xhtml+xml. They say:

They are completely wrong.

XHTML document served as application/xhtml+xml has the same DOM as XHMTL served as text/html.

At first Mozilla has been creating XMLDocument object for pages served as application/xhtml+xml, but then there was opened a Bug in December 2001, and the first fix was proposed in March 2003 and in two monthes final fix was accepted. Since that time Mozilla started creating HTMLDocument object for XHTML pages served as application/xhtml+xml and therefore they had to support all that HTML stuff: document.write, document.applets, HTMLElement.innerHTML, document.forms, document.anchors, document.images, document.links, document.cookie

The only thing they didn't support is document.all, it's only supported in quirks mode of document served as text/html.

As for Opera: it checks namespace of element to understand if it is xhtml and therefore there should be a HTML DOM or if it is a xml document and there should be XML DOM. And yes, Opera provides HTML DOM for ALL MIME types that XHTML can be served as.

XHTML served as application/xml or text/xml

Mozilla creates XMLDocument for XHTML served as application/xml or text/xml, so it doesn't provide HTML DOM for such documents. But Opera does.

innerHTML

Yes, that may sound strange, but it's a fact - innerHTML is supported everywhere, FOR EVERY XHTML MIME-TYPE.

Conclusion

There's quite little amount of problems concerned XHTML served as application/xhtml+xml, javascript works fine since there's the same HTML DOM as it would have being served as text/html. When you serve XHTML as text/html or application/xhtml+xml, in both case you will have eqaul HTML DOMs. So don't be afraid of all those scary stories of not working innerHTML or document.forms - it's all a lie.

For those who don't believe me.

comments powered by Disqus