Page 1 of 1

document.write with XHTML

PostPosted: Fri Apr 05, 2013 12:53 pm
by RSteinwand
Hi Albert,

Couldn't help but notice the new big, red warning with v. 12. :mrgreen:

I'm using it to write my mobile/desktop css and a script for only desktop users and haven't noticed any problems.

This is the doctype I'm using, (which doesn't have the goofy <XML> tag I've seen suggested):

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
   <meta http-equiv="content-type" content="text/html" />


I'm wondering if it's with certain other doctypes or if this should be a warning instead of an error.

I am using innerHTML for other areas where the code can be backfilled after page load.

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 1:06 pm
by RSteinwand
I wonder if the issue is only for "application/xhtml+xml" content-type?

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 1:23 pm
by Albert Wiersch
Hi Rick,

My understanding is that XHTML is based on XML, which has strict parsing rules. Inserting content into a document with document.write() is a big "NO NO" as XML parsers are not designed to handle it.

You can, of course, disable the message or "tone it down" to a warning using CSE HTML Validator's configuration options.

Or, better yet may be to change the document to HTML5 if you don't need XML parsing.

Here's some information from Mozilla about document.writeln and XHTML:
https://developer.mozilla.org/en-US/doc ... nt.writeln

Which points to this link:
http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

Does document.write work in XHTML?

No. Because of the way XML is defined, it is not possible to do tricks like this, where markup is generated by scripting while the parser is still parsing the markup.

You can still achieve the same effects, but you have to do it by using the DOM to add and delete elements.


I will improve the message to give more details about this issue.

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 1:40 pm
by RSteinwand
Hi Albert,

I think this is the key part of your link:

When an XHTML page is served with MIME type text/html it is treated by all browsers as if it were nothing more than HTML. However when an XHTML page is served with MIME type text/xml or application/xhtml+xml, then it should be treated as an XML document which must conform to the strict rules for authoring and displaying XML.


My pages are text/html via the content-type and also via server headers.

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 2:02 pm
by Albert Wiersch
Hi Rick,

I see your point, but I'm not sure what the best solution is. If a browser treats an XHTML document as HTML, then document.write() should work, but the underlying document is still an XHTML document (or should be) even though the browser may not treat it as such.

I think the best solution may be to leave it as an error and let the developer "tone it down to a warning or regular message" or ignore it completely if they want.

It may also be possible to check the MIME type (if the document was requested with HTTP), and if it's available and if it's text/html, then CSE HTML Validator could handle it a bit more leniently (perhaps a warning instead - and change the message ID so the message could be disabled independently from when this is not the case).

What are your thoughts on handling this?

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 2:17 pm
by RSteinwand
I found out a LOT more about this after my initial posting. I should have investigated it first. :D

You could treat it as an error if you find "application/xhtml+xml" and as a warning with "text/html", but I suspect leaving it as-is might be best.

Re: document.write with XHTML

PostPosted: Fri Apr 05, 2013 3:34 pm
by Albert Wiersch
Thanks Rick. I'll just leave it as is for now. I did try to make the message more informative though:

Because of the way XML is defined and parsed, XHTML documents should not use document.write() or document.writeln(), but it appears that this has been used in this script. XML (and therefore XHTML) does not allow for markup to be generated while the parser is still parsing the markup. To add and delete elements, use the DOM and innerHTML property instead.