什么是HTML中的CDATA? [英] What is CDATA in HTML?

查看:160
本文介绍了什么是HTML中的CDATA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript标签和HTML中使用CDATA是什么?

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

//]]>
< / script>


解决方案

XML文档中的所有文本都将被解析器。



但CDATA段中的文本将被解析器忽略。


$ b CDATA - (未解析)字符数据


术语CDATA用于不应被XML解析器解析的文本数据。 / p>

字符如<和&在XML元素中是非法的。

<会生成一个错误,因为解析器将其解释为新元素的开始。



&会产生一个错误,因为解析器将它解释为一个字符实体的开始。



有些文本,如JavaScript代码,包含很多<或&字符。为避免错误,脚本代码可以定义为CDATA。



解析器忽略CDATA节中的所有内容。



< CDATA部分以<![CDATA [结尾并以]]>> 结尾

在程序输出中使用CDATA


如果XHTML文档中的CDATA段将文档呈现为HTML,则XHTML文档中的CDATA段可能会被网页浏览器以不同方式进行解析,因为HTML解析器不识别CDATA开始和结束标记,也不识别HTML实体引用例如< script> 标签内的& lt; 。这可能会导致Web浏览器出现渲染问题,并且如果用于显示来自不受信任源的数据,可能会导致跨站点脚本漏洞,因为这两种解析器会不同意CDATA部分结束。


简短的SGML教程



另请参阅在CDATA上维基百科条目


What is the use of CDATA inside JavaScript tags and HTML?

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

// ]]>
</script> 

解决方案

All text in an XML document will be parsed by the parser.

But text inside a CDATA section will be ignored by the parser.

CDATA - (Unparsed) Character Data

The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>"

Use of CDATA in program output

CDATA sections in XHTML documents are liable to be parsed differently by web browsers if they render the document as HTML, since HTML parsers do not recognise the CDATA start and end markers, nor do they recognise HTML entity references such as &lt; within <script> tags. This can cause rendering problems in web browsers and can lead to cross-site scripting vulnerabilities if used to display data from untrusted sources, since the two kinds of parsers will disagree on where the CDATA section ends.

A brief SGML tutorial.

Also, see the Wikipedia entry on CDATA.

这篇关于什么是HTML中的CDATA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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