I have this piece of code:
<script language="javascript" type="text/jscript">
document.write("<img src='http://dm.leadgenesys.com/jpgp.lgt?en=P.........TP_Q=&ur=' + escape(document.referrer) + '' border='0' alt='no alt' />");
</script>
and... when I try to validate it, I'm given this error:
document type does not allow element "img" here
…rer) + '" border="0" alt="no alt" />');
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).
Any idea what I can do to make this JavaScript w3c compliant?
It's part of the HTML4 standard. You must escape "</" sequences found inside SCRIPT tags.
Another solution, and probably better, is to move the JS code in external files.
Reference
How aboout
See that the content of the "script" is inside "html comments"?
You probably have your
<script>
element in your<head>
, so essentially you're trying to create an<img>
in the head of your document, which doesn't work. You need to put the script in the<body>
.However, I'd recommend you add the element via the DOM instead, because you'll be less likely to run into these kinds of problems.
Simple: don't try to validate your JavaScript as HTML.
You can do this in a number of ways... But the best by far is to move it out into a separate JS file, and then either call it from a short script
...or better yet, ditch
document.write()
entirely and manipulate the DOM after it has loaded.See also: Unobtrusive JavaScript
Or:
Another way to silence the validator...
Put it like this:
The CDATA part should be enough for the validator, the /* style comments */ are for older browsers which do not recognize the CDATA tag (it would otherwise break the javascript).