JavaScript usage in XHTML-basic knowledge

Source: Internet
Author: User
Tags cdata
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.