Piwik and XHTML 5: Document.write and Noscript

I’ve been using Piwik recently for my site analytic purposes. I added it to my “professional” site, which is served as XHTML 5 for anything but IE. On that site, no visits were registering, though awstats showed that there were visitors. As it happens, this is because the javascript “document.write” is not allowed in XHTML. I believe older versions of XHTML still allowed it to be run, but it certainly wasn’t being run on my XHTML 5 site. Firefox showed an error in the console. This is mentioned on the WHATWG’s page about the differences between HTML5 and XHTML5.

The Piwik community doesn’t seem to have much mention of this, other than one mailing list thread. I modified the script to something similar to the one in that thread, like this:

<!-- Piwik -->
<div id="piwikbox"></div>
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://example.com/piwik/" : "http://example.com/piwik/");
var tag = document.createElement("script");
tag.setAttribute("src", pkBaseURL + "piwik.js");
tag.setAttribute("type", 'text/javascript');

</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
} catch( err ) {}

This inserts an element with the appropriate content into the DOM instead of writing it directly to the page, which would be considered the preferred way these days anyway.

You might notice that I’ve also removed the “noscript” element normally used by Piwik to add an image that will make a call to the stats server when javascript is disabled. This is because the “noscript” element is not allowed in XHTML 5. It was allowed in XHTML 1. But XML does not allow conditional parsing: All elements are parsed and added to the DOM. So browsers simply set the content to “display: none;” and everything looked fine, but the elements were still parsed, and if you were to put a script into the “noscript” tags, it would be run in XHTML, though not in HTML. Form inputs would be submitted in XML, though not in HTML (I’ve been told anyway). I have to wonder why it really matters that XML must parse these elements: The elements are just elements until the browser takes them and renders them in a certain way. To an XML client that doesn’t have special rendering for “input” elements, they are just empty elements with some attributes. The browser could be directed to render and handle the “noscript” content differently even though it is still parsed and added to the DOM, beyond just “display: none;”. But of course old browsers will still handle them as they do now. This would work fine in Piwik’s case, but then it really doesn’t matter currently for Piwik anyway, as that functionality has not actually been implemented yet anyway. So it’s just removed.

All this trouble makes me wonder why I should bother with XHTML 5 anyway. Browsers simply give an often useless warning when there’s an error in the markup, and don’t display the page at all. Firefox at least doesn’t even let you view the source, making it very difficult to figure out where the issues is. IE cannot render it properly and must be sent a different MIME and doctype. HTML 5 can be basically the same but without some of the constraints. However, I like the XML syntax and would rather serve my document as the type of content it is marked up as. I like to know that there are well-formedness errors, even if I’d prefer them to be handled differently. I like being able to easily use my same layout in an XSLT, such as for my xml sitemap. For clients, I will use HTML, since I don’t want their site to stop working because of a small markup error. But for my own site, I will continue to use XTML.

Published by


I am a quiet person from Northeast Ohio. I work as a web developer. I like computers, music, and many other things.

5 thoughts on “Piwik and XHTML 5: Document.write and Noscript”

  1. Test new Piwik Tracking code without document.write
    Try the following change: http://dev.piwik.org/trac/ticket/2517
    and then report if it is working (and by email if possible)?
    The noscript element must not be used in XML documents.
    The noscript element is only effective in the HTML syntax, it has no effect in the XHTML5 syntax.
    Your website http://tobymackenzie.com/ has the following errors XHTML5
    1: Bad value sitemap for attribute rel on XHTML element link: Keyword sitemap is not registered.
    2: Bad value webmaster technicalauthor author for attribute rel on XHTML element a: Keyword webmaster is not registered.
    Can you please try the following validator: http://validator.nu/?doc=http%3A%2F%2Ftobymackenzie.com%2F

    1. Ah, I’m almost positive those “rel” attribute values validated before. The one (“sitemap”) is listed now as “proposed” and the other two no longer exist (“webmaster” and “technicalauthor”).

      I’ve modified the linked change to remove the “noscript” element and the “type” attributes on the “script” tags. Seems to work. I will email you as well.

Leave a Reply

Your email address will not be published. Required fields are marked *