Possible Duplicate:
Why does this Javascript code inside a non-Javascript browser have extra commenting?
有时我会看到一些以 <!--
开头的 Javascript并以 //-->
结尾.
我想知道这样的线有什么用?据我所知,<!--
表示注释行。但事实似乎并非如此,因为即使使用这样的行,脚本仍然可以工作。如果删除该行,它也有效。
这是一个简短的例子:
<script language='JavaScript' type='text/javascript'>
<!--
function changelang(id) {
document.location.href='index.php&lch=1&lang=' + id }
//-->
</script>
它曾经用于不理解的旧浏览器script
标签并将其内容节点呈现为文本,这当然是不可取的。
当您想到 canvas
的内容时,这可能更有意义, audio
, noscript
元素(后者在今天被特殊对待,但想像一下过去,浏览器不知道 noscript
元素是什么)。
在当今时代,这不是必需的。
Douglas Crockford 建议不要使用它们。
Do not use the <!-- //-->
hack with scripts. It was intended to
prevent scripts from showing up as text on the first generation
browsers Netscape 1 and Mosaic. It has not been necessary for many
years. <!-- //-->
is supposed to signal an HTML comment. Comments
should be ignored, not compiled and executed. Also, HTML comments are
not to include --
, so a script that decrements has an HTML error.
Source .