URL in <script> tags fails W3C test? [SOLVED]

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

URL in <script> tags fails W3C test? [SOLVED]

Post 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
Last edited by Stryks on Sun Nov 05, 2006 5:30 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Just remembered that using a CDATA section also avoids the error, if memory serves.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Umm, comments so that it's not parsed as markup?

Code: Select all

<script type="text/javascript">
<!-- Comment

....

// -->
</script>
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post 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. :D
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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?
Post Reply