The rules for writing XHTML code are much stricter than writing HTML, and code like the following is valid in HTML, but not in XHTML.
[JavaScript]
Copy Code code as follows:
<script type= "Text/javascript" >
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
</script>
<script type= "Text/javascript" >
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
</script>
In HTML, there are special rules to determine which content in the <script> element can be parsed, but these rules do not apply in XHTML. Because the less-than sign (<) will be interpreted as starting a new label in XHTML. However, as a label, the less-than number cannot be followed by a space, thus causing a syntax error.
There are two solutions: first, replace all the less than (<) in the code with the corresponding HTML entity (<), and use a CDATA fragment to contain the JavaScript code.
Method A corresponding code:
[JavaScript]
Copy Code code as follows:
<script type= "Text/javascript" >
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
</script>
<script type= "Text/javascript" >
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
</script>
Method two corresponding code:
[JavaScript]
Copy Code code as follows:
<script type= "Text/javascript" ><! [cdata[
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
]]></script>
<script type= "Text/javascript" ><! [cdata[
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
]]></script>
Method one can make code work properly in XHTML, but it causes code to be poorly understood, and method two solves the problem in XHTML-compliant browsers. However, many browsers are not compatible with XHTML and therefore do not support CDATA fragments. So use JavaScript annotations to annotate the CDATA tags.
Appropriate code:
[HTML]
Copy Code code as follows:
<script type= "Text/javascript" >
<! [cdata[
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
]]>
</script>
<script type= "Text/javascript" >
<! [cdata[
function Compare (A, B)
{
if (a < b)
{
Alert ("A is less then B");
}
else if (a > B)
{
Alert ("A is greater then B");
}
Else
{
Alert ("A is equal to B");
}
}
]]>
</script>
This format is available in all modern browsers.
Attached: deprecated syntax
[JavaScript]
Copy Code code as follows:
<script><!--
function Sayhi () {
Alert ("hi!");
}
--></script>
<script><!--
function Sayhi () {
Alert ("hi!");
}
--></script>
Like the above, including JavaScript code in an HTML annotation allows browsers that do not support <script> elements to hide embedded JavaScript code, which ignores the contents of <script> tags. In the case of JavaScript-enabled browsers, you must further confirm that it contains JavaScript code that needs to be parsed.
Although this annotation format is recognized by all browsers and can be interpreted correctly, it is not necessary to use this format since all browsers already support JavaScript.