Forum Moderators: open

Message Too Old, No Replies

embedded javascript in xhtml

         

urfriend

3:35 pm on Sep 13, 2007 (gmt 0)

10+ Year Member



I have an xhtml document, which i tried to validate, and it seems to give me an error when it sees the '<' symbol inside my javascript.

Does anyknow know how to do this and make it xhtml 1.1 compliant?


<script type="text/javascript">
for( i=0 ; i<len ; i++) {
blah blah blah;
}
</script>

When it gets to the i<len part, the validator gives me an error:

element "len" undefined.

Marshall

9:18 am on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Remove the script, re-validate and if everything is fine, put the script back in. It is a no win situation as far as I know.

Marshall

penders

11:49 am on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...put the script back in. It is a no win situation as far as I know.

Well, it depends if it is important that your page is true XHTML and not simply HTML.

The validator is correct, in that you cannot embed unescaped script (as you are doing using '<' and '&' etc.) in your XHTML pages. The thing is with XHTML is if it doesn't validate then it's not XHTML! In a strict sense. The browser, however, is likely to be handling this by displaying it as HTML, not XHTML.

This is a big difference between XHTML and HTML. In HTML your script is considered CDATA (character data), in XHTML it's PCDATA (Parsed Character Data). The XHTML processor treats '<' and '&' etc. as markup.

You have a few options...

[1] Use HTML instead. (Why most of us should NOT use XHTML [webmasterworld.com])

[2] Move your JS into an external JS file and perhaps call it from the windows.onload event. It is always good practise to have your JS in an external file... separating your content and behaviours.

[3] If you only have a line or two of script then you can enclose your script in a CDATA section (to avoid it being parsed by XHMTL). (See [w3.org...]

<![CDATA[ 
.....
]]>

But then the CDATA section itself needs to be commented out to avoid it being processed by the JS engine (in browsers that don't support XHTML, ie. IE!), and this is where it gets a bit messy, I can never remember the correct syntax. Although, having had a look around, it may be as simple as:

<script type="text/javascript"> 
//<![CDATA[
for( i=0 ; i<len ; i++) {
blah blah blah;
}
//]]>
</script>

Please check it - I've not tried this recently.

I'm sure I've seen/used far more complicated escapes in the past, although this I suspect was to accommodate very old browsers (which most of us can now ignore?) which required the use of HTML comments <!-- --> (in addition to those mentioned) to hide the script from browsers that didn't even acknowledge the script tag.

Useful (Mozilla) resource...
Properly_Using_CSS_and_JavaScript_in_XHTML_Documents [developer.mozilla.org]

Marshall

12:16 pm on Sep 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



urfriend, sorry I forgot about the CDATA (brain freeze). Thanks Penders

Marshall