为什么CDATA是根据脚本标签注释掉? [英] Why does CDATA is commented out under script tags ?

查看:278
本文介绍了为什么CDATA是根据脚本标签注释掉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在读这问题,我有一个相关的问题:

I was reading this question and I have a related question :

这家伙 这里 说:

它在脚本标记用来避免解析<并与功放;.在HTML中,这是
  没有必要的,因为在HTML中,脚本的 #CDATA。

It is used in script tags to avoid parsing < and &. In HTML, this is not needed, because in HTML, script is already #CDATA.

<大骨节病>问题1

如果脚本的的#CDATA:为什么它renderd(在脚本标签)仍CDATA?

If SCRIPT is already #CDATA : why does it renderd (under script tag) still as CDATA ?

<script type="text/javascript"> 
// <![CDATA[

// ]]>
</script> 

<大骨节病>问题2

和为什么它作为一个评论? ( //

And why is it as a comment ? (//)

推荐答案

XHTML应该通过媒体类型担任XML 是application / xhtml + XML 。在HTML5中,标记为 XHTML如果送达一个XML媒体类型。当担任这样,脚本元素的内容的不可以 CDATA。

XHTML is supposed to be served as XML by using media type application/xhtml+xml. In HTML5, the markup is only XHTML if it is served with an XML media type. When served like this, the contents of script elements are not CDATA.

因此​​,要获得XML解析器来处理脚本内容为CDATA,它们可以被包裹在&LT;![CDATA []]&GT;

So to get the XML parser to treat the script contents as CDATA, they can be wrapped in <![CDATA[ ]]>.

虽然很少人在历史上担任标记为是application / xhtml + XML ,很多已经验证自己的网页,就好像它是XHTML。在XHTML验证同样预计,脚本内容不一般CDATA,所以通常会拒绝嵌入JavaScript代码和标记等下脚料,除非它们是用&LT逃脱;![CDATA []]&GT ;

While few people have historically served markup as application/xhtml+xml, many have validated their pages as if it was XHTML. The XHTML validator equally expects that the script contents are not ordinarily CDATA, and so will typically reject tags and other scraps of markup embedded in the JavaScript, unless they are escaped with <![CDATA[ ]]>

已经证明了他们的页面作为XHTML,那么他们会为他们的网页以的text / html 媒体类型的浏览器,这意味着该浏览器将标记作为HTML ,不是XHTML。在这种情况下,HTML解析器使用,这确实的自动处理脚本内容为CDATA,因此&LT;![CDATA []]&GT; 。成为脚本的一部分由JavaScript引擎运行。因此,要隐藏的JavaScript引擎这些字符串,它们是在同一行,这意味着使用JavasScript引擎认为行是注释preceded与 //

Having validated their pages as XHTML, they'd then serve their pages with a text/html media type to browsers, which meant that the browser treats the markup as HTML, not XHTML. In this case, the HTML parser is used, which does treat the script contents as CDATA automatically, so the <![CDATA[ and ]]>. become part of the script to be run by the JavaScript engine. Therefore, to hide those strings from the JavaScript engine, they are preceded with // on the same line, which means that the JavasScript engine thinks the lines are comments.

最后,有些人服务于相同的标记既是application / xhtml + XML 的text / html ,开关基于在HTTP请求消息中的信息。为与上述相同的理由,以获得脚本内容来在两种模式下正确地进行处理,在 //&下;![CDATA [ / /]方式&gt; 模式是一种非常有效的技术。

Finally, some people serve the same markup as both application/xhtml+xml and text/html, switching based on the information found in the HTTP request message. For the same reasons as above, to get the script contents to be processed correctly in both modes, the //<![CDATA[ and //]]> pattern is a very effective technique.

这篇关于为什么CDATA是根据脚本标签注释掉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆