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

前端 未结 5 586
被撕碎了的回忆
被撕碎了的回忆 2020-12-06 05:30

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

if (user_age > 15 && user_age < 91)

It gets the following errors:

相关标签:
5条回答
  • 2020-12-06 05:53

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

    0 讨论(0)
  • 2020-12-06 05:54

    All Javascript should be CDATA in XHTML:

    <![CDATA[
    if (user_age > 15 && user_age < 91)
    ]]>
    
    0 讨论(0)
  • 2020-12-06 05:57

    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.

    0 讨论(0)
  • 2020-12-06 06:01

    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> 
    
    0 讨论(0)
  • 2020-12-06 06:02

    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.

    0 讨论(0)
提交回复
热议问题