DOM Extreme Tracker

Following on from Lori Leach's question on the WSG list:

I want to put a "hit tracker" on my site -- well actually, I already have one. I have been using Extreme Tracker to keep up with stats. However, a few weeks ago, when I posted something up here, a few of you went to my site, validated my code and it was a mess -- this was due to the ET code.

The default Extreme Tracker code relies on a <SCRIPT> and a <NOSCRIPT> block, which could be made to validate by escaping ampersands and wrapping parts in proper block level elements. More fundamentally, though, the code relies on document.write() which won't work if the document is sent as application/xhtml+xml (see Hixie's Natural Log: Why document.write() doesn't work in XML)

<a href=""
target="_top"><img src=""
name="EXim" border="0" height="38" width="41"
alt="eXTReMe Tracker"></img></a>
<script type="text/javascript" language="javascript1.2"><!--
</script><script type="text/javascript"><!--
var EXlogin='reduxphl' // Login
var EXvsrv='s9' // VServer
EXd.write("<img src=\"",
"l="+escape(EXd.referrer)+"\" height=1 width=1>");//-->
</script><noscript><img height="1" width="1" alt=""

In order to get this to work in XHTML aware browsers when sending the document with the correct MIME type, the entire <SCRIPT> section was first removed, leaving only the <IMG> element of the <NOSCRIPT> block, with an added ID.

<img id="extremetracker" height="1" width="1" alt="" src=";j=n&amp;jv=n"/>

Next, the javascript was changed, so that the SRC attribute of this remaining image is dynamically rewritten on page load. All that is required now to get the tracker to work are the static image with the ID and a call to the script from the document's <HEAD>.

<script type="text/javascript" src="extreme_tracker.js"></script>

eXTReMe Tracker


As Bert Doorn rightly points out, though, older, non DOM capable browsers will not process this script, skewing the results. D'oh! Blame it on the fact that I don't actually use this type of tracker, relying on server-side log, in essence, this was an exercise in "DOMification", if nothing else, as the end result may not give the expected results the same way the old, kludgey script does.