XHTML won't validate && and < in a JavaScript function

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.

Answers


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> 

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.


All Javascript should be CDATA in XHTML:

<![CDATA[
if (user_age > 15 && user_age < 91)
]]>

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


you can try CDATA but some time it wont work, it depends on the setting of the server I guess. I am not a pro, but i tested, and I did not work, but if you put the javascript code in the .js file and then link this file somewhere in your body. it will definitely work. PERSONALLY TESTED.


Need Your Help

Get the decimal part from a double

c# .net

I want to receive the number after the decimal dot in the form of an integer. For example, only 05 from 1.05 or from 2.50 only 50 not 0.50

Firefox error rendering an SVG image to HTML5 canvas with drawImage

javascript firefox svg html5-canvas base64

I am trying to convert an external svg icon to a base64 png using a canvas. It is working in all browsers except Firefox, which throws an error "NS_ERROR_NOT_AVAILABLE".