gpt4 book ai didi

javascript - Chrome 中极其奇怪的故障 - 解析字符串的内容!

转载 作者:搜寻专家 更新时间:2023-10-31 22:35:43 24 4
gpt4 key购买 nike

好的 - 这是我一段时间以来看到的最愚蠢的故障:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type='text/javascript'>

var data = "</script>";

</script>
</head>
<body>

This should break!

</body>
</html>

这会导致语法错误,因为 JavaScript 解析器实际上正在读取字符串的内容。多么愚蠢!

我怎样才能把</script>在我的代码中。有什么办法吗?

这种行为是否有正当理由?

最佳答案

在 X(HT)ML 中(当 actually treated as such 时),正是出于这个原因,脚本需要作为 CDATA 进行转义。 http://www.w3.org/TR/xhtml1/diffs.html#h-4.8

In XHTML, the script and style elements are declared as having #PCDATA content. As a result, < and & will be treated as the start of markup, and entities such as &lt; and &amp; will be recognized as entity references by the XML processor to < and & respectively. Wrapping the content of the script or style element within a CDATA marked section avoids the expansion of these entities.

<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>

如果您的 XHTML 文档只是作为 text/html 提供并被视为标签汤,那不适用,您只需要“转义”字符串,如 '</scr' + 'ipt>' .

关于javascript - Chrome 中极其奇怪的故障 - 解析字符串的内容!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2696592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com