/    Sign up×
Community /Pin to ProfileBookmark

validating xhtml with Javascript

I am trying to have my XHTML Transitional page validated using the w3 validator: [URL=”http://validator.w3.org/”]http://validator.w3.org/[/URL]

However, I have used Java script on the page and am getting errors from it. Mainly, it has problems with one line,

[code]for (i = 0; i <len; i++) {[/code]

and also the close </script> tag, saying it is not correctly nested, but it is <script>…code…</script> and no other html tags in between. However, it is considering “len” (in the code above) as an element and therefore says I must first close it.

The len line is getting errors such as: “;” is not allowed, “len” is an undefined element, XML parsing errors etc.

How do you have Javascript ignored by XHTML validators/browsers. I know you can use in strings but these are not strings.

Any ideas?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@blue-eye-labsJan 16.2008 — the validator doesn't seem to recognise javascript as non-html, which was a problem I had. I recommend sticking your javscript in like so:

[code=html]
blah blah blah
<script type="text/javscript" language="javascript">
<!--//hides for non-js browsers (and validators)
scriptomatic();
//-->
</script>
blah blah blah
[/code]


The HTML comments within the script tag prevent the validator reading the script (and also prevent non-js browsers doing so). The // at end-comment block is so that javascript won't try and treat --> as a function &c...

If that fails, put your js in an external document and link it using
[code=html]
<script language="javscript" src="/path/to/document.js">
...
</script>
[/code]


I hope that helps.
Copy linkTweet thisAlerts:
@Jeff_MottJan 16.2008 — XHTML requires that you mark your scripts as CDATA, or character data (see http://www.w3.org/TR/html/#h-4.8). It would look something like this:&lt;script type="text/javascript"&gt;
&lt;![CDATA[
... unescaped script content ...
]]&gt;
&lt;/script&gt;


But to be honest, it's just all around better if you keep your scripts in an external JS file.
Copy linkTweet thisAlerts:
@felgallJan 16.2008 — <len is invalid in XHTML unless it is contained inside a CDATA tag and so your validation is reporting it correctly.
Copy linkTweet thisAlerts:
@acidedge2004authorJan 17.2008 — I used the CDATA tags, and while it did mean the page passed validation, the script will now not work. Is the cdata tags only supposed to be there during validation?
Copy linkTweet thisAlerts:
@KorJan 17.2008 — even the CDATA markup must be commented, because of the [B]<[/B] and [B]>[/B] characters, which are interpreted by the XML as start/end of tag markup...
<i>
</i>&lt;script type="text/javascript"&gt;
[COLOR="Blue"]/*[/COLOR]&lt;![CDATA[[COLOR="Blue"]*/[/COLOR]
[COLOR="DimGray"]...... code here .......[/COLOR]
[COLOR="Blue"]/*[/COLOR]]]&gt;[COLOR="Blue"]*/[/COLOR]
&lt;/script&gt;
Copy linkTweet thisAlerts:
@acidedge2004authorJan 17.2008 — Thanks Kor, works great now.
×

Success!

Help @acidedge2004 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...