1) The JavaScript version is not going to work for a great number of users so don't use it.
2) That example of JavaScript is incompatible with XHTML. In XHTML the content of the SCRIPT element is PCDATA, just like any other element. The parser is still looking for tags so when it hits those in the document.write methods things get wonky. When it hits the less than sign things break. For that reason and others document.write and document.writeln are not supported by XHTML browsers.
3) That example of JavaScript is also incompatible with HTML. In HTML the SCRIPT and STYLE elements are a special case and they are defined as containing CDATA. The parser stops looking for tags. However, the sign that the CDATA has ended and that it's time for the parser to resume looking for tags is the ETAGO, '</'. And that piece of script contains several of those.
3) Its not even valid XHTML. The namespace designation is missing.