views:

379

answers:

6

Here's the snippet of code that won't validate:

if (user_age > 15 && user_age < 91)

It gets the following errors:

XML Parsing Error: StartTag: invalid element name

and

XML Parsing Error: xmlParseEntityRef: no name

The first error is thrown for the "less than" and the second one is thrown twice, once for each ampersand.

Replacing the above signs with "&" and "<" validates fine, but of course it completely ruins the function.

Any help appreciated.

+5  A: 

Move script to other file :)

It is standard (and good) habit to separate style (into .css file), data (.html) and of course scripts to .js file.

Rin
css should also be in it's own file
MrChrister
just send update :)
Rin
+7  A: 

Or you can protect the script from the xml validation like this:

<script type="text/javascript"> 
//<![CDATA[
    if (user_age > 15 && user_age < 91) {
        // do soemthing
    }
//]]>
</script>
Joel Coehoorn
kaizer.se
A: 

put javascript in <![CDATA[...]]> section

Xinus
As stated in a reply to Martin's answer: Don't forget to comment it out as <![CDATA[]]> is an XML literal in JavaScript.
Eli Grey
+1  A: 

All Javascript should be CDATA in XHTML:

<![CDATA[
if (user_age > 15 && user_age < 91)
]]>
Martin
Don't forget to comment it out as <![CDATA[]]> is an XML literal in JavaScript.
Eli Grey
A: 

You could use HTML instead, seeing as IE for one doesn't understand XHTML.

Tim Down
A: 

I've had the same problem and CDATA method didn't workk for me, so what I did was change de && in the ifs for nested ifs

if(a==1 && b==2){something}

became

if(a==1) { if(b==2) { something } }

vellonce