JS in//<! [cdata[meaning

Source: Internet
Author: User
Tags cdata

CDATA Everything inside is ignored by the parser, and the added text contains a large number of < and $ symbols, as is often the case in programming, so this element can be defined as a CDATA section, andCDATA sections begin with "<![ Cdata[", End at"]]> ":

Why use this?

XHTML the second change is to use CDATA segments, which areused to declare text that should not be parsed as labels (XHTML is also the case), so that special characters such as less than (<), greater than (>), and Number (&), and double quotation marks (") are used. Instead of having to use their character entities

Here's how to formally add CDATA segments:

<script type= "Text/javascript" ><! [cdata[function  Compare (b) {if (a < b)   {alert ("A less than B");} Else if (a>B)   {Alert ("a greater than B");} Else    {alert ("a equals B");}]] ></script>

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

<script type= "Text/javascript" >//<![ cdata[                                            function  Compare (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 solution currently in use mimics the method of hiding code from the old browser. Use a single-line JavaScript comment "//" to embed a CDATA segment without affecting code syntax:

This code can now be run in browsers that do not support XHTML.

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

JS in//<! [cdata[meaning

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.