2

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=&amp;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?

1
  • The language attribute is deprecated and there's no text/jscript value for the type attribute. Well, there's not text/javascript either, but that's what browsers are used to see right there. Commented Jun 4, 2009 at 22:17

6 Answers 6

5

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

<body>
...
<script type="text/javascript" language="javascript">WriteImage();</script>
...
</body>

...or better yet, ditch document.write() entirely and manipulate the DOM after it has loaded.

See also: Unobtrusive JavaScript

Sign up to request clarification or add additional context in comments.

1 Comment

the separated js file worked great for me! since the <script> wasn´t on the <head> thank you!
5

Another way to silence the validator...

Put it like this:

<script type="text/javascript">
/* <![CDATA[ */
your_javascript_here("<" + ... ;
/* ]]> */
</script> 

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).

5 Comments

Perhaps I'm confused, Shouldn't it be: //<![CDATA[ //]]>
CDATA sections only work when the document is parsed as XML (sent using application/xhtml+xml)
These are multiline comments, as in C.
Miles is right, that CDATA crap means nothing for the browser. And by the way, in real XHTML there's no document.write.
I agree it means nothing to the browser, that's why i wrote 'another way to silence (read:trick) the validator...' :)
2

How aboout

<script>
<!--
var i = null;
if ((i/0) == 12)
  alert("whooo! pack your things, as it's starting to rain cats and dogs!");
--><script>

See that the content of the "script" is inside "html comments"?

Comments

2

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.

Comments

1

It's part of the HTML4 standard. You must escape "</" sequences found inside SCRIPT tags.

<SCRIPT type="text/javascript">
  document.write ("<EM>This will work<\/EM>")
</SCRIPT>

Another solution, and probably better, is to move the JS code in external files.

Reference

2 Comments

That's not the problem, as there's no "</" sequence in his script tag. It's still true, though.
Miles, you're right. It slipped my eyes that he's using an IMG tag.
0
document.write("<" + "img … /" + ">");

Or:

document.write("\x3Cimg … /\x3E");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.