脚本标记中什么时候需要 CDATA 部分?

发布于 2024-07-04 13:51:38 字数 297 浏览 7 评论 0原文

脚本标签中是否需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

冧九 2024-07-11 13:51:38

CDATA表示其中的内容不是XML。

CDATA indicates that the contents within are not XML.

内心荒芜 2024-07-11 13:51:38

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

坏尐絯℡ 2024-07-11 13:51:38

CDATA表示其中的内容不是XML。

这是维基百科的解释

CDATA indicates that the contents within are not XML.

Here is an explanation on wikipedia

埋情葬爱 2024-07-11 13:51:38

当您要严格遵循 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.

╭⌒浅淡时光〆 2024-07-11 13:51:38

以避免 xhtml 验证期间出现 xml 错误。

to avoid xml errors during xhtml validation.

多像笑话 2024-07-11 13:51:38

CDATA 告诉浏览器按原样显示文本,而不是将其呈现为 HTML。

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

戴着白色围巾的女孩 2024-07-11 13:51:38

它确保当您将 JavaScript 嵌入到页面中而不是外部引用时,XHTML 验证正常工作。

XHTML 要求您的页面严格符合 XML 标记要求。 由于 JavaScript 可能包含具有特殊含义的字符,因此您必须将其包装在 CDATA 中,以确保验证不会将其标记为格式错误。

<块引用>

对于网络上的 HTML 页面,您只需在 和 标记之间包含所需的 JavaScript。 当您验证网页上的 HTML 时,JavaScript 内容被视为 CDATA(字符数据),因此会被验证器忽略。 如果您在设置网页时遵循更新的 XHTML 标准,则情况并非如此。 对于 XHTML,脚本标记之间的代码被视为 PCDATA(已解析的字符数据),因此由验证器进行处理。

因此,您不能仅在页面上的脚本标记之间包含 JavaScript,而不“破坏”您的网页(至少就验证器而言)。

您可以在此处了解有关 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.

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.

谎言月老 2024-07-11 13:51:38

不要在 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 >.

一人独醉 2024-07-11 13:51:38

这是 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.

迷迭香的记忆 2024-07-11 13:51:38

基本上它是允许编写 XHTML 和 HTML 文档。 问题是在 XHTML 中,XML 解析器将解释 &,<,> script 标签中的字符并导致 XML 解析错误。 因此,您可以使用实体编写 JavaScript,例如:

if (a > b) alert('hello world');

但这是不切实际的。 更大的问题是,如果您阅读 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.:

if (a > 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.

初与友歌 2024-07-11 13:51:38

HTML

HTML 解析器会将 之间的所有内容视为脚本的一部分。 有些实现甚至不需要正确的结束标签; 它们在“”处停止脚本解释,根据规格

更新在 HTML5 和当前的浏览器中,情况不再是这样了。

因此,在 HTML 中,这是不可能的:

<script>
var x = '</script>';
alert(x)
</script>

CDATA 部分根本没有效果。 这就是为什么你需要写

var x = '<' + '/script>'; // or
var x = '<\/script>';

或类似的内容。

这也适用于作为 text/html 的 XHTML 文件。 (由于 IE 不支持 XML 内容类型,所以这基本上是正确的。)

XML

在 XML 中,应用不同的规则。 请注意,如果 XHMTL 文档提供 XML 内容类型,(非 IE)浏览器仅使用 XML 解析器。

对于 XML 解析器来说,script 标签并不比任何其他标签更好。 特别地,脚本节点可以包含非文本子节点,由“<”触发; “&”符号表示字符实体。

因此,在 XHTML 中,这是不可能的:

<script>
if (a<b && c<d) {
    alert('Hooray');
}
</script>

要解决此问题,您可以将整个脚本包装在 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 specs.

Update In HTML5, and with current browsers, that is not the case anymore.

So, in HTML, this is not possible:

<script>
var x = '</script>';
alert(x)
</script>

A CDATA section has no effect at all. That's why you need to write

var x = '<' + '/script>'; // or
var x = '<\/script>';

or 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:

<script>
if (a<b && c<d) {
    alert('Hooray');
}
</script>

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 a CDATA section anyway.

小清晰的声音 2024-07-11 13:51:38

当浏览器将标记视为 XML 时:

<script>
<![CDATA[
    ...code...
]]>
</script>

当浏览器将标记视为 HTML 时:

<script>
    ...code...
</script>

当浏览器将标记视为 HTML 并且您希望验证 XHTML 1.0 标记(例如)时。

<script>
//<![CDATA[
    ...code...
//]]>
</script>

When browsers treat the markup as XML:

<script>
<![CDATA[
    ...code...
]]>
</script>

When browsers treat the markup as HTML:

<script>
    ...code...
</script>

When browsers treat the markup as HTML and you want your XHTML 1.0 markup (for example) to validate.

<script>
//<![CDATA[
    ...code...
//]]>
</script>
与酒说心事 2024-07-11 13:51:38

如果您需要将文档解析为 XML(例如,当 XHTML 页面被解释为 XML 时)并且您希望能够编写文字 i<10一&& b 而不是 i<10a && 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 and a && b instead of i<10 and a && 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

情话已封尘 2024-07-11 13:51:38

这样,旧版浏览器就不会解析 Javascript 代码,页面也不会中断。

向后兼容性。 一定要喜欢它。

That way older browser don't parse the Javascript code and the page doesn't break.

Backwards compatability. Gotta love it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文