When is a CDATA section necessary within a script tag?

后端 未结 15 2267
时光说笑
时光说笑 2020-11-21 22:37

Are CDATA tags ever necessary in script tags and if so when?

In other words, when and where is this:



        
相关标签:
15条回答
  • 2020-11-21 22:54

    It to ensure that XHTML validation works correctly when you have JavaScript embedded in your page, rather than externally referenced.

    XHTML requires that your page strictly conform to XML markup requirements. Since JavaScript may contain characters with special meaning, you must wrap it in CDATA to ensure that validation does not flag it as malformed.

    With HTML pages on the web you can just include the required JavaScript between and tags. When you validate the HTML on your web page the JavaScript content is considered to be CDATA (character data) that is therefore ignored by the validator. The same is not true if you follow the more recent XHTML standards in setting up your web page. With XHTML the code between the script tags is considered to be PCDATA (parsed character data) which is therefore processed by the validator.

    Because of this, you can't just include JavaScript between the script tags on your page without 'breaking' your web page (at least as far as the validator is concerned).

    You can learn more about CDATA here, and more about XHTML here.

    0 讨论(0)
  • 2020-11-21 22:55

    Do not use CDATA in HTML4 but you should use CDATA in XHTML and must use CDATA in XML if you have unescaped symbols like < and >.

    0 讨论(0)
  • 2020-11-21 22:56

    CDATA tells the browser to display the text as is and not to render it as an HTML.

    0 讨论(0)
  • 2020-11-21 23:02

    Basically it is to allow to write a document that is both XHTML and HTML. The problem is that within XHTML, the XML parser will interpret the &,<,> characters in the script tag and cause XML parsing error. So, you can write your JavaScript with entities, e.g.:

    if (a &gt; b) alert('hello world');
    

    But this is impractical. The bigger problem is that if you read the page in HTML, the tag script is considered CDATA 'by default', and such JavaScript will not run. Therefore, if you want the same page to be OK both using XHTML and HTML parsers, you need to enclose the script tag in CDATA element in XHTML, but NOT to enclose it in HTML.

    This trick marks the start of a CDATA element as a JavaScript comment; in HTML the JavaScript parser ignores the CDATA tag (it's a comment). In XHTML, the XML parser (which is run before the JavaScript) detects it and treats the rest until end of CDATA as CDATA.

    0 讨论(0)
  • 2020-11-21 23:02

    It's an X(HT)ML thing. When you use symbols like < and > within the JavaScript, e.g. for comparing two integers, this would have to be parsed like XML, thus they would mark as a beginning or end of a tag.

    The CDATA means that the following lines (everything up unto the ]]> is not XML and thus should not be parsed that way.

    0 讨论(0)
  • 2020-11-21 23:06

    CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript. This is also the reason why JSLint complains about the < character in regexes.

    References

    • Creating a declarative XML UI language
    • The Future of the Web: Rich Clients, Rich Browsers, Rich Portals
    0 讨论(0)
提交回复
热议问题