Since making the effort to get Joomla! working with strict XHTML and serving the correct MIME type accordingly, I've been a little annoyed that my W3Counter stats stopped recording the referrer links for those pages served as XHTML (i.e. for all non-IE vistors, which is 90% or more of the people who find their way here).
It just goes to show how few people are actually serving pages for a real XML DOM still, that almost all of the assorted widgets and various tracker scripts being thrown around, either rely on iframes or at least try to lazily document.write(foo) into your page, which (understandably) is not taken kindly to by the XML parser, producing errors like this:
Error: uncaught exception: [Exception... "Operation is not supported" code: "9" nsresult: "0x80530009 (NS_ERROR_DOM_NOT_SUPPORTED_ERR)" location: "http://www.w3counter.com/tracker.js Line: 108"]
So, after Google and the W3Counter site repeatedly failed to turn up any useful information (or even mention) about this, I decided to try hacking the tracker script myself.
The offending lines are at the end of the tracker.js:
if (randomnumber <= 5) { document.write('<a href="http://www.w3counter.com"><img src="http://www.w3counter.com/tracker.php?id=' + id + info + '" style="border: 0" alt="W3Counter Web Stats" /></a>'); } else { document.write('<a href="http://www.w3counter.com"><img src="http://track.w3counter.com/tracker.php?id=' + id + info + '" style="border: 0" alt="W3Counter Web Stats" /></a>'); }
Now, you can't document.write into a XHTML DOM, presumably because that would output more "raw" text needing to be parsed, right in the middle of constructing the DOM for the first lot, but that doesn't mean you can't manipulate the DOM in more structured ways that don't create another job for the parser at an inopportune moment.
So what I decided to do is alter the HTML tracking code to include directly the tracker site's link tag having ID "w3counter" (instead of writing that from the javascript) then just drop the W3Counter image link containing all the tracked data (e.g. referrer) as parameters in the query string, into the DOM as it's child from the javascript code:
<!-- Begin W3Counter Tracking Code --> <a id="w3counter" href="http://www.w3counter.com" /> <script type="text/javascript" src="/includes/js/w3counter.js"></script> <script type="text/javascript">w3counter(12345);</script> <!-- End W3Counter Tracking Code-->
NB: If copying this remember to replace 12345 with your own W3Counter ID number! Refer to comments below for why the <noscript> block can't be included for XHTML.
Obviously, you can't alter the original tracker.js in situ, so I made a copy called w3counter.js under the joomla /includes/js with the modified code to add the tracking image in a XHTML-friendly way:
if (randomnumber <= 5) { var src='http://www.w3counter.com/tracker.php?id=' + id + info; } else { var src='http://track.w3counter.com/tracker.php?id=' + id + info; } if(document.createElementNS) var w3=document.createElementNS("http://www.w3.org/1999/xhtml","img"); else // play nice with retarded browsers var w3=document.createElement("img"); w3.setAttribute("src", src); w3.setAttribute("style", "border: 0"); w3.setAttribute("alt", "W3Counter Web Stats"); document.getElementById("w3counter").appendChild(w3);
It all seems to be working fine now, as best I can tell.
PS: I'm just assuming that making alterations like this doesn't violate any "Terms of Service" or other such legal nonsense...I don't see why W3Counter would mind, but you never know with these things, so let's just be clear that I disclaim any liability, blah blah blah.





