Technology: CDATA In XHTML (extensible HTML) standards

Source: Internet
Author: User
Tags cdata
xhtml| Standard

Recently, with the advent of XHTML (extensible HTML) standards,,<script/> tags have undergone some changes. The label no longer uses the language attribute, and the type attribute declares either inline code or the MIME type of the external file to be added, and JavaScript mime is "Text/javascript". For example:

<title>title of Page</title>
<script type= "Text/javascript" >
var i = 0;
</script>
<script type= "Text/javascript" src= ". /scripts/external.js "></script>
<body>
<!--body goes-->
</body>

Even though many browsers do not fully support XHTML, most developers now use the type feature instead of language features to provide better XHTML support. Omitting the language feature does not cause any problems because, as previously mentioned, all browsers default to <script/> This property value is JavaScript.

The second change in XHTML is the use of CDATA segments. CDATA Segments in XML are used to declare text that should not be parsed into labels (as is the case with XHTML), so that special characters such as less than (<), greater than (>), and (&) and double quotes ("") are used without having to use their character entities. Consider the following code:

<script type= "Text/javascript" >
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</script>

This function is fairly simple, it compares numbers a and B, and then displays a message stating their relationship. However, in XHTML, this code is invalid because it uses three special symbols, which are less than, greater than, and double quotes. To fix this problem, you must replace them with the three-character XML entity <, >, and ":

<script type= "Text/javascript" >
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
131
JavaScript in the Browser
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
</script>

There are two problems with this piece of code. First, developers are not accustomed to writing code with XML entities. This makes the code difficult to read. Second, in JavaScript, this code will actually be considered a syntax error because the interpreter does not know the meaning of the XML entity. Using CDATA segments, you can write JavaScript code in a regular form (that is, readable syntax). The way to formally add a CDATA section is as follows:

<script type= "Text/javascript" >
<! [cdata[
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
]]>
</script>

While this is a formal approach, remember that most browsers do not fully support XHTML, which leads to the main problem that this is a syntax error in JavaScript because most browsers do not recognize CDATA segments.

The solution that is currently in use mimics the method of hiding the old browser code. Using a single-line javascript annotation, you can embed a CDATA segment without affecting code syntax:

<script type= "Text/javascript" >
<! [cdata[
function Compare (A, b) {
if (a < b) {
Alert ("A is less than B");
else if (a > B) {
Alert ("A is greater than B");
} else {
Alert ("A is equal to B");
}
}
]]>
</script>

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

Show hidden content

As with the type attribute, CDATA This usage is becoming more prevalent as developers prepare for better support for XHTML in the browser. However, to avoid CDATA problems, it is best to introduce JavaScript code with external files.



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.