脚本标记中什么时候需要 CDATA 部分?
脚本标签中是否需要 CDATA 标签?如果需要,何时需要?
换句话说,何时何地这样:
<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>
比这样更好:
<script type="text/javascript">
...code...
</script>
Are CDATA tags ever necessary in script tags and if so when?
In other words, when and where is this:
<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>
preferable to this:
<script type="text/javascript">
...code...
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
CDATA表示其中的内容不是XML。
CDATA indicates that the contents within are not XML.
CDATA 在任何 XML 方言中都是必需的,因为 XML 节点中的文本在被计算为 JavaScript 之前会被视为子元素。 这也是 JSLint 抱怨正则表达式中的
<
字符的原因。参考
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
CDATA表示其中的内容不是XML。
这是维基百科的解释
CDATA indicates that the contents within are not XML.
Here is an explanation on wikipedia
当您要严格遵循 XHTML 时,您需要 CDATA,这样小于号和与号就不会被标记为无效字符。
When you are going for strict XHTML compliance, you need the CDATA so less than and ampersands are not flagged as invalid characters.
以避免 xhtml 验证期间出现 xml 错误。
to avoid xml errors during xhtml validation.
CDATA 告诉浏览器按原样显示文本,而不是将其呈现为 HTML。
CDATA tells the browser to display the text as is and not to render it as an HTML.
它确保当您将 JavaScript 嵌入到页面中而不是外部引用时,XHTML 验证正常工作。
XHTML 要求您的页面严格符合 XML 标记要求。 由于 JavaScript 可能包含具有特殊含义的字符,因此您必须将其包装在 CDATA 中,以确保验证不会将其标记为格式错误。
您可以在此处了解有关 CDATA 的更多信息,以及有关 XHTML 的更多信息请参见此处。
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.
You can learn more about CDATA here, and more about XHTML here.
不要在 HTML4 中使用 CDATA,但您应该在 XHTML 中使用 CDATA,并且如果您有未转义的符号(例如 ),则必须在 XML 中使用 CDATA。 和>。
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 >.
这是 X(HT)ML 的事情。 当您在 JavaScript 中使用
<
和>
等符号时,例如比较两个整数,必须像 XML 一样进行解析,因此它们将标记为开始或标签的末尾。CDATA 意味着以下几行(直到
]]>
的所有内容都不是 XML,因此不应以这种方式进行解析。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.基本上它是允许编写 XHTML 和 HTML 文档。 问题是在 XHTML 中,XML 解析器将解释 &,<,> script 标签中的字符并导致 XML 解析错误。 因此,您可以使用实体编写 JavaScript,例如:
但这是不切实际的。 更大的问题是,如果您阅读 HTML 页面,标签 script 会被“默认”视为 CDATA,这样的 JavaScript 将不会运行。 因此,如果您希望同一页面同时使用 XHTML 和 HTML 解析器,则需要将 script 标记包含在 XHTML 中的 CDATA 元素中,但不要将其包含在 HTML 中。
这个技巧将 CDATA 元素的开始标记为 JavaScript 注释; 在 HTML 中,JavaScript 解析器会忽略 CDATA 标记(它是注释)。 在 XHTML 中,XML 解析器(在 JavaScript 之前运行)检测它并将 CDATA 结尾之前的其余部分视为 CDATA。
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.:
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.
HTML
HTML 解析器会将
和
之间的所有内容视为脚本的一部分。
有些实现甚至不需要正确的结束标签; 它们在“”处停止脚本解释,根据规格
。
因此,在 HTML 中,这是不可能的:
CDATA
部分根本没有效果。 这就是为什么你需要写或类似的内容。
这也适用于作为
text/html
的 XHTML 文件。 (由于 IE 不支持 XML 内容类型,所以这基本上是正确的。)XML
在 XML 中,应用不同的规则。 请注意,如果 XHMTL 文档提供 XML 内容类型,(非 IE)浏览器仅使用 XML 解析器。
对于 XML 解析器来说,
script
标签并不比任何其他标签更好。 特别地,脚本节点可以包含非文本子节点,由“<
”触发; “&
”符号表示字符实体。因此,在 XHTML 中,这是不可能的:
要解决此问题,您可以将整个脚本包装在
CDATA
部分中。 这告诉解析器:“在本节中,不要将“<
”和“&
”视为控制字符。” 为了防止 JavaScript 引擎解释“”和“
]]>
”标记,您可以将它们包含在注释中。如果您的脚本不包含任何“
<
”或“&
”,则无论如何您都不需要CDATA
部分。HTML
An HTML parser will treat everything between
<script>
and</script>
as part of the script.Some implementations don't even need a correct closing tag; they stop script interpretation at ".</
", which is correct according to the specsSo, in HTML, this is not possible:
A
CDATA
section has no effect at all. That's why you need to writeor similar.
This also applies to XHTML files served as
text/html
. (Since IE does not support XML content types, this is mostly true.)XML
In XML, different rules apply. Note that (non IE) browsers only use an XML parser if the XHMTL document is served with an XML content type.
To the XML parser, a
script
tag is no better than any other tag. Particularly, a script node may contain non-text child nodes, triggered by "<
"; and a "&
" sign denotes a character entity.So, in XHTML, this is not possible:
To work around this, you can wrap the whole script in a
CDATA
section. This tells the parser: 'In this section, don't treat "<
" and "&
" as control characters.' To prevent the JavaScript engine from interpreting the "<![CDATA[
" and "]]>
" marks, you can wrap them in comments.If your script does not contain any "
<
" or "&
", you don't need aCDATA
section anyway.当浏览器将标记视为 XML 时:
当浏览器将标记视为 HTML 时:
当浏览器将标记视为 HTML 并且您希望验证 XHTML 1.0 标记(例如)时。
When browsers treat the markup as XML:
When browsers treat the markup as HTML:
When browsers treat the markup as HTML and you want your XHTML 1.0 markup (for example) to validate.
如果您需要将文档解析为 XML(例如,当 XHTML 页面被解释为 XML 时)并且您希望能够编写文字
i<10
和一&& b 而不是
i<10
和a && b
,因为 XHTML 会将 JavaScript 代码解析为已解析的字符数据,而不是默认的字符数据。 对于存储在外部源文件中的脚本来说,这不是问题,但对于 XHTML 中的任何内联 JavaScript,您可能希望使用 CDATA 部分。请注意,许多 XHTML 页面从未打算被解析为 XML,在这种情况下这不会成为问题。
有关该主题的精彩文章,请参阅 https://web.archive.org/web/20140304083226/http://javascript.about.com/library/blxhtml.htm
A CDATA section is required if you need your document to parse as XML (e.g. when an XHTML page is interpreted as XML) and you want to be able to write literal
i<10
anda && b
instead ofi<10
anda && b
, as XHTML will parse the JavaScript code as parsed character data as opposed to character data by default. This is not an issue with scripts that are stored in external source files, but for any inline JavaScript in XHTML you will probably want to use a CDATA section.Note that many XHTML pages were never intended to be parsed as XML in which case this will not be an issue.
For a good writeup on the subject, see https://web.archive.org/web/20140304083226/http://javascript.about.com/library/blxhtml.htm
这样,旧版浏览器就不会解析 Javascript 代码,页面也不会中断。
向后兼容性。 一定要喜欢它。
That way older browser don't parse the Javascript code and the page doesn't break.
Backwards compatability. Gotta love it.
当您希望其验证时(XML/XHTML - 谢谢,Loren Segal)。
When you want it to validate (in XML/XHTML - thanks, Loren Segal).