Page 1 of 1
URL in <script> tags fails W3C test? [SOLVED]
Posted: Sat Nov 04, 2006 4:17 am
by Stryks
I have some javascript which holds a few URL's for a rotating link which is failing the W3C HTML validator.
Code: Select all
<script type="text/javascript">
var content=new Array()
content[0]='<a href="http://www.url1.com">URL 1</a><br />Description for URL 1.'
content[1]='<a href="http://www.url2.com">URL 1</a><br />Description for URL 2.'
content[2]='<a href="http://www.url3.com">URL 1</a><br />Description for URL 3.'
</script>
Am I doing something wrong here, or is it just a false positive?
Cheers
Posted: Sat Nov 04, 2006 6:58 am
by feyd
The only thing that's popping out to me is missing semicolons. But it would appear to be false positives. You should be able to adjust how they are stored, or better yet convert them to use the DOM to build themselves without too much trouble.
Posted: Sat Nov 04, 2006 10:18 pm
by Stryks
In case it clarifies things, here is what it is reporting for each line:
Error Line 67 column 46: document type does not allow element "a" here.
pausecontent[0]='<a href="http://www.url1.com">URL 1</a><br />Description for URL 1.'
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).
One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
Error Line 67 column 61: document type does not allow element "br" here.
...content[0]='<a href="http://www.url1.com">URL 1</a><br />Description for URL 1.'
These are all specified in <script> tags in the body just out of interest.
feyd - I'm interested in your suggestion to fix it, but I'm not sure how to implement. Any chance of some more information or an example?
Thanks
Posted: Sat Nov 04, 2006 10:29 pm
by feyd
The DOM way would use
document.createElement() and
element.appendChild() multiple times.
If you need an example usage of both, I posted a key tracking toy a while ago.
viewtopic.php?t=37059
Posted: Sun Nov 05, 2006 10:17 am
by AKA Panama Jack
It's a false positive. The W3C validator has problems when you are using javascript in the HTML page. It tags things with errors that never should be tagged. One way around this is to load the javascript by using <script src="yourscript.js"></script> in the <head></head> tags.
Posted: Sun Nov 05, 2006 11:17 am
by feyd
Just remembered that using a CDATA section also avoids the error, if memory serves.
Posted: Sun Nov 05, 2006 11:23 am
by Chris Corbyn
Umm, comments so that it's not parsed as markup?
Code: Select all
<script type="text/javascript">
<!-- Comment
....
// -->
</script>
Posted: Sun Nov 05, 2006 12:10 pm
by AKA Panama Jack
I don't know if that will help on the W3C validator. Those <!-- --> tags were for older browsers that didn't support javascript so the code would be hidden.
Posted: Sun Nov 05, 2006 5:29 pm
by Stryks
Well ... there ya go.
The
Code: Select all
<script type="text/javascript">
<!-- Comment
....
// -->
</script>
dealy did the trick.
Script still works and validator passes it without any problems.
Thanks for the help guys.

Posted: Mon Nov 06, 2006 5:16 am
by Maugrim_The_Reaper
Panama Jack is right.
The W3C result is a false positive. Your HTML was perfectly fine assuming that was the only error. Commenting out the script is only required to prevent older browsers from kicking up a mess. It's not required.
Does anyone know if this occurs under the XHTML Validation tests? Or is it a HTML 4.0 issue only?