gpt4 book ai didi

javascript - 这是非法语法吗?

转载 作者:行者123 更新时间:2023-11-28 16:26:50 24 4
gpt4 key购买 nike

<script type="text/javascript" language="JavaScript">
<!--
alert('foo');
//-->
</script>

它在我公司的 grails 应用程序中到处使用,但我知道 <是非法的 JavaScript 字符...

应该<!--就像 //<!--相反?

最佳答案

不,这并不违法。来自 the specification :

The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line.

然而,这是毫无意义的。它旨在阻止无法识别脚本元素的浏览器将内容呈现为文本。自 Netscape 2 起就支持 script 元素!这使得今天使用该语法完全毫无意义。

但是,如果您使用 XHTML,那么它是非常有害的。由于脚本元素不包含内在的 CDATA,因此它们内部的标记被视为标记而不是 CDATA,因此它实际上会将脚本注释掉(如果文档被处理为 XHTML 而不是标签 soup,这将需要 application/xhtml+ xml 内容类型)。

Should the <!-- be like //<!-- instead?

不,那会(部分)击败该对象。

关于javascript - 这是非法语法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7732306/

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