Significance of JavaScript CDATA

Source: Internet
Author: User

Everything inside CDATA is ignored by the parser.

Assume that the text contains a large number of "<" and "&" characters-like programmingCodeThis is often the case-the XML element can be defined as a CDATA part.

CDATA section starts with "<! [CDATA [", ended at"]> ":

<SCRIPT type = "text/JavaScript">
<! [CDATA [
Function compare (A, B)
{
If (A <B)
{Alert ("a less than B ");}
Else if (A> B)
{Alert ("a greater than B ");}
Else
{Alert ("A equals B ");}
}
]>
</SCRIPT>

In the preceding example, everything in the CDATA section is ignored by the parser.

CDATA section comment:

CDATA segments cannot contain strings "]>". Therefore, CDATA segment Nesting is not allowed.

At the same time, make sure that there are no spaces or lines in the "]>" string.

Why use CDATA:

The second change to XHTML is to use CDATA segments. The CDATA segment in XML is used to declare text that should not be parsed as tags (as is XHTML), so that special characters such as less than (<) and greater than (>) can be used), and signs (&) and double quotation marks (") without using their character entities. Consider the following code:

<SCRIPT type = "text/JavaScript">
Function compare (A, B)
{
If (A <B)
{Alert ("a less than B ");}
Else if (A> B)
{Alert ("a greater than B ");}
Else
{Alert ("A equals B ");}
}
</SCRIPT>

This function is quite simple. It compares numbers A and B, and then displays the message to describe their relationship. However, in XHTML, this code is invalid because it uses three special characters, namely, less than, greater than, and double quotation marks. To fix this problem, you must replace them with the XML entities & lt;, & gt; and & quot; of the three characters:

<SCRIPT type = "text/JavaScript">
Function compare (A, B)
{
If (A & lt; B)
{Alert (& quot; a less than B & quot ;);}
Else if (A & gt; B)
{Alert (& quot; a greater than B & quot ;);}
Else
{Alert (& quot; A equals B & quot ;);}
}
</SCRIPT>

This code has two problems. First, developers are not used to writing code using XML entities. This makes the code hard to understand. Second, in Javascript, such code is actually considered as a syntax error becauseProgramI do not know the meaning of XML objects. You can use CDATA segments to write JavaScript code in the regular form (that is, the easy-to-read syntax. To formally Add a CDATA segment, follow these steps:

<SCRIPT type = "text/JavaScript">
<! [CDATA [
Function compare (A, B)
{
If (A <B)
{Alert ("a less than B ");}
Else if (A> B)
{Alert ("a greater than B ");}
Else
{Alert ("A equals B ");}
}
]>
</SCRIPT>

Although this is a formal method, remember that most browsers do not fully support XHTML, which leads to a major problem, that is, this is a syntax error in JavaScript, because most browsers do not know CDATA segments.

<SCRIPT type = "text/JavaScript">
// <! [CDATA [
Function compare (A, B)
{
If (A <B)
{Alert ("a less than B ");}
Else if (A> B)
{Alert ("a greater than B ");}
Else
{Alert ("A equals B ");}
}
//]>
</SCRIPT>

The current solution imitates the "hide the old Browser" code method. Use a single line of JavaScript comment "//" to embed CDATA segments without affecting the code syntax:

Now, this code can be run in browsers that do not support XHTML.

However, to avoid CDATA problems, it is best to use external files to introduce JavaScript code.

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.