Home Articles HowTo Guides HowTo: Make W3Counter Work In XHTML

HowTo: Make W3Counter Work In XHTML

E-mail Print PDF

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.

Last Updated on Wednesday, 23 September 2009 17:55  

Comments

Spoke too soon... from Will Daniels on Wed. Sep 23, 2009  reply 

I noticed some duplication in the W3Counter logs after making this change. Stripping all the pWidget stuff out of the modified tracker.js seemed to clear up most of that. It seems to reference yet another javascript file on the w3counter site itself so I guess that's what caused duplicate vist reporting somehow. I did not know about that pWidget thing actually and it seems quite a nice feature so I will spend some more time on this and fix it properly soon, but right now all I wanted really was to get my referrer stats back, so this can wait.

RE: Spoke too soon... from Will Daniels on Wed. Sep 23, 2009  reply 

Now it's looking like the pWidget stuff has nothing to do with the matter. After commenting out the <noscript> tag, the duplicates are gone. I guess XHTML behaves differently somehow where noscript is concerned - it must actually fetch the noscript image regardless. I'll have to look into that further.

RE: HowTo: Make W3Counter Work In XHTML from Will Daniels on Wed. Sep 23, 2009  reply 

I found some info about the behavior of <noscript> in XHTML here which seems to explain the duplication issue:

"In HTML, if scripting is enabled, the noscript element is parsed as an CDATA element. If scripting is disabled, it's parsed as a normal element. In XHTML, the element is always parsed as a normal element, and can't really be used to stop content from being present when script is disabled."