Usage of JavaScript in XHTML

Source: Internet
Author: User
Tags html comment

The rules for writing XHTML code are much stricter than those for compiling HTML code. Similar code below is valid in HTML, but it is invalid in XHTML.
[Javascript]
Copy codeThe Code is 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 used to determine which content in the <script> element can be parsed, but these rules are not applicable in XHTML. Because less than sign (<) will be parsed as a new tag in XHTML. However, as a tag, the tag cannot be followed by spaces, which leads to syntax errors.
There are two solutions: 1. Use the corresponding HTML Entity (<) to replace all the minor signs (<) in the Code; 2. Use a CData segment to include JavaScript code.
Method 1 code:
[Javascript]
Copy codeThe Code is 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 2 code:
[Javascript]
Copy codeThe Code is 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>

Although method 1 can make the code run normally in XHTML, it makes the code hard to understand. method 2 can solve the problem in a browser compatible with XHTML. However, many browsers are not compatible with XHTML and therefore do not support CData fragments. So use JavaScript annotations to comment out the CData mark.
Code:
[Html]
Copy codeThe Code is 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 can be used normally in all modern browsers.
Appendix: Unsupported syntax
[Javascript]
Copy codeThe Code is as follows:
<Script> <! --
Function sayHi (){
Alert ("Hi !);
}
// --> </Script>
<Script> <! --
Function sayHi (){
Alert ("Hi !);
}
// --> </Script>

As shown in the preceding figure, adding JavaScript code to an HTML comment allows browsers that do not support <script> elements to hide embedded JavaScript code, that is, ignore the content in the <script> tag, in this case, browsers that support JavaScript must further confirm whether it contains the JavaScript code to be parsed.

Although this annotation format is recognized by all browsers and can be correctly interpreted, it is unnecessary to use it because all browsers support JavaScript.

Related Article

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.