document.write method is not allowed in well-formed XHTML pages – as described at W3C in XHTML FAQ: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.
However, document.write is used everywhere so if you still rely on some older non-XHTML-aware scripts I’ll show you how to give your legacy code a DOM-facelift.
<!--
d=document;a='';a+=';r='+escape(d.referrer)
js=10//--><!--
a+=';j='+navigator.javaEnabled()
js=11//--><!--
s=screen;a+=';s='+s.width+'*'+s.height
a+=';d='+(s.colorDepth?s.colorDepth:s.pixelDepth)
js=12//--><!--
js=13//--><!--
d.write('<a href="http://top.mail.ru/jump?from=341994"'+
' target=_top><img src="http://top.list.ru/counter'+
'?id=341994;t=84;js='+js+a+';rand='+Math.random()+
'" alt="Rating@Mail.ru"'+' border=0 height=18 width=88>'+'')
if(js>11)d.write('<a
target=_top href="http://top.mail.ru/jump?from=341994"><img
src="http://top.list.ru/counter?js=na;id=341994;t=84"
border=0 height=18 width=88
alt="Rating@Mail.ru"></a><!--
if(js>11)d.write('--'+'>')//-->
The only word to describe such code is: BAD! There is no way it’s going to be validated as anything more strict than HTML 4.01 Transitional, and even then it is plain ugly. Let’s beautify it and replace document.write (this example uses d=document;d.write() construct for short) with something that will validate as pure XHTML 1.1.
First, numerous <!-- and --> comments will have to go – they give my JavaScript console in Firefox the creeps. Second, the lines starting with if(js>11)d.write seem redundant – what they do is, basic`ly, escape the block if the code is rendered by JavaScript engine of certain version, which is omitted automatically if JS is available, anyway! And while we’re in the mood for cleaning, let’s remove target property of <a> tag and border from . And then we shall double-quote the rest of properties and put the final slash (”/”) in tag.
One more thing: the path in is unique but <a href="http://top.mail.ru/jump?from=341994"> part is static and thus appears twice – in and in – for no good use. We will move it outside, thus saving on code length and avoiding redundancy. Here is what we shall get:
<a
href="http://top.mail.ru/jump?from=341994">
d=document;a='';a+=';r='+escape(d.referrer)
js=10
a+=';j='+navigator.javaEnabled()
js=11
s=screen;a+=';s='+s.width+'*'+s.height
a+=';d='+(s.colorDepth?s.colorDepth:s.pixelDepth)
js=12
js=13
d.write('<img src="http://top.list.ru/counter'+
'?id=341994;t=84;js='+js+a+';rand='+Math.random()+
'" alt="Rating@Mail.ru"'+' height="18" width="88" />')
<img
src="http://top.list.ru/counter?js=na;id=341994;t=84" height="18" width="88"
alt="Rating@Mail.ru" /></a>
This code certainly looks much better – so if you’re not interested in full XHTML 1.1 compatibility and just need the corrected code for Mail.ru counter, well, there you go. And we shall move on too.
I used the code shown by Mark Pilgrim in his article, The Road to XHTML 2.0:if (document.getElementById) {
var l=document.createElementNS("http://www.w3.org/1999/xhtml","link");
l.setAttribute("rel", "stylesheet");
l.setAttribute("type", "text/css");
l.setAttribute("href", "/css/js.css");
l.setAttribute("media", "screen");
document.getElementsByTagName("head")[0].appendChild(l);
}The code above puts a reference for CSS-file into document’s What we need is to create a similar line for our tag. Here is the code identical to the one above:if (document.getElementById) {
var i=document.createElement("img");
i.src="http://top.list.ru/counter?id=341994;t=84;js="+Math.random()+js+a;
i.width=88;
i.height=18;
i.alt="Rating@Mail.ru";
}But it lacks one critical part, the one with childAppend, which will put our tag in its place. However, we’ll have no use of the inside the . We might get something like this: document.getElementsByTagName("body")[0].appendChild(l);Note however that this way the tag is going to be rendered at the very bottom of the page which is not exactly our intention.But we use a different method instead of
document.getElementsByTagName – document.getElementsById! And keeping in mind that we now have an anchor tag around the block we will give it a unique ID and then point appendChild at it:
<a href="http://top.mail.ru/jump?from=341994" id="m">d=document;a='';a+=';r='+escape(d.referrer)
js=10
a+=';j='+navigator.javaEnabled()
js=11
s=screen;a+=';s='+s.width+'*'+s.height
a+=';d='+(s.colorDepth?s.colorDepth:s.pixelDepth)
js=12
js=13
if (d.getElementById) {
var i=d.createElement("img");
i.src="http://top.list.ru/counter?id=341994;t=84;js="+Math.random()+js+a;
i.width=88;i.height=18;i.alt="Rating@Mail.ru";
d.getElementById("m").appendChild(i);
}
<img src="http://top.list.ru/counter?js=na;id=341994;t=84"
height="18" width="88" alt="Rating@Mail.ru" /></a>
Congratulations, we now have a valid XHTML-friendly JavaScript! The result can be seen in action on my sidebar.
