Click to See Complete Forum and Search --> : using < and & in script with xhtml


LittleRed
11-04-2003, 03:08 AM
I've just gone through the W3C specification for using xhtml and it mentions that '< and & will be treated as the start of markup, ...' within script or style elements.

I don't quite follow what they suggest (wrapping it within <![CDATA[ ...), but does this mean if I have
if ((condition) && (other_condition)) {
that odd things will happen after the first '&' ?

cheers

Fang
11-04-2003, 03:19 AM
They mean this:
<script type="text/javascript">
//<![CDATA[
<!--

//-->
//]]>
</script>

Gollum
11-04-2003, 03:19 AM
In layman's terms treating '<' and '&' as start of markup means that the XML/XHTML/whatever parser recognises those characters as having special meaning and not just plain character data. For instance '<' is typically the start of a tag - when you write <br> in a web page you are saying add a new line here, you don't want the browser to actually display "<br>" on the screen.

Now what happens if you do want to display or send those characters and not have them recognised by the parser. Well in XML terms that means telling the parser that some characters are not to be parsed. That's what the <![CDATA[ ... ]]> does. Everything inside this funny looking node is ignored by the parser so you can use < and & as much as you like.

LittleRed
11-04-2003, 03:20 AM
is it worth doing Fang's code for all occurrences of JavaScript within xhtml then?

Gollum
11-04-2003, 03:22 AM
Yes, given '<' and '&' are used quite a lot in javascript

Fang
11-04-2003, 03:26 AM
Compulsory! BUT, ideally Javascript should be in one included script file. Separate content from layout.

LittleRed
11-04-2003, 03:28 AM
thanks for your help!