[英]Why does <!— return undefined and not a syntax error?
The html comment tags <!--
& -->
return undefined
when run as a js command, I expected a syntax error. html注释标签
<!--
& -->
作为js命令运行时返回undefined
,我期望语法错误。 Why does this happen ? 为什么会这样?
I stumbled upon this in DoubleClick ... (the download link). 我在DoubleClick ... ( 下载链接)中偶然发现了这一点。
Because <script>
was added to HTML as an afterthought, and at the time MANY browsers didn't acknowledge the existence of scripts, <!--
is actually a defined member of the Javascript language spec, and is treated as "start of comment". 由于
<script>
是后来才添加到HTML的,并且当时许多浏览器都不承认脚本的存在,因此<!--
实际上是Javascript语言规范的已定义成员,被视为“注释的开始”。 ”。
Remember that by default, browsers ignore tags that they do not understand, so that 请记住,默认情况下,浏览器会忽略他们不理解的标签,因此
<tag_which_does_not_exist>hi there</tag_which_does_not_exist>
would actually display "hi there" in a browser. 实际上会在浏览器中显示“ hi there”。 For script-unaware browsers, that'd mean they'd actuall display the JS code as text in the document.
对于不支持脚本的浏览器,这意味着它们实际上会将JS代码显示为文档中的文本。 So..
所以..
<script>
<!--
alert('hi there');
// -->
</script>
would pop up a JS alert in script-aware browsers, and would be completely ignored by script-ignorant browsers. 会在可识别脚本的浏览器中弹出JS警报,而对脚本无知的浏览器将完全忽略它。
Also note that -->
is NOT valid Javascript, which is why it has to be entered as // -->
. 还要注意
-->
不是有效的Javascript,这就是为什么必须将其输入为// -->
。 //
is the other JS single-line comment, and it comments out the otherwise invalid -->
html end-of-comment tag. //
是另一个JS单行注释,它注释掉了否则无效的-->
html注释结束标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.