我应该使用HTML注释来封装JavaScript代码块吗? [英] Should I use HTML comments to capsulate JavaScript code blocks?

查看:78
本文介绍了我应该使用HTML注释来封装JavaScript代码块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我被告知HTML中嵌入的JavaScript代码块应始终封装在HTML注释中,如下所示:

Some years ago, I was taught that JavaScript code blocks embedded inside HTML should always be capsulated inside HTML comments as following:

<script type="text/javascript">
<!--
var hello = "world";
-->
</script>

我被告知这样做,但我从来没有完全弄明白为什么。使用HTML注释似乎很麻烦,所以现在我开始使用在脚本块中编写我的JavaScript代码而没有HTML注释:

I was told to do this, but I never kind of fully figured out why. It kind of seems hacky to use HTML comments, so nowadays I have started using writing my JavaScript code inside the script block without the HTML comments:

<script type="text/javascript">
var hello = "world";
</script>

所以我的问题是:我应该使用HTML注释封装JavaScript代码块吗?在没有评论的情况下编写脚本是否安全?我的意思是,当我遗漏评论标签时,我冒了一些风险?

So my question is: should I use HTML comments to capsulate JavaScript code blocks? Is it safe to just write the script without the comments? I mean am I risking something when I leave out the comment tags?

推荐答案

HTML评论旨在隐藏JavaScript中的JavaScript浏览器不理解< script> 元素,而是在页面上呈现其内容。这可以追溯到90年代中期,iirc。 现在您可以放心地假设那个时代的浏览器不再出现在网络上并且省略了评论。

The HTML comment was intended to hide the JavaScript from ancient browsers that didn't understand the <script> element and instead render its contents on the page. That was way back in the mid-90es, iirc. Nowadays you can safely assume that browsers from that era are no longer present on the web and omit the comments.

一些不错的历史就可以了找到这里

Some nice history on that can be found here:


关于浏览器不理解的HTML标记的一般规则是浏览器应该完全忽略标记并将页面内容视为该标记不存在。这意味着当Netscape 2首次引入JavaScript(或当时称为LiveScript)时,有必要在实际脚本周围放置HTML注释,以隐藏其他不了解脚本标记的浏览器的代码,以及因此会显示代码而不是运行代码。

The general rule regarding HTML tags that browsers do not understand is that the browser should ignore the tag completely and treat the content of the page as if that tag were not there. This means that when Netscape 2 first introduced JavaScript (or LiveScript as it was called then), it was necessary to place an HTML comment around the actual script in order to hide the code from other browsers that didn't understand the script tag and which therefore would have displayed the code rather than running it.

JavaScript语言专门用于接受HTML注释的开头,这是脚本中的第一件事,并且忽略它,以便HTML注释可以用来隐藏使用Netscape 1,Mozaic,Internet Explorer 1,Internet Explorer 2和其他类似浏览器的人的脚本,其中任何人都不再使用它们。正是这些史前浏览器(用JavaScript术语)意味着当您在过时的JavaScript教程中看到将JavaScript包装在HTML注释中以便将其从旧浏览器中隐藏时的参考。

The JavaScript language was specifically written to accept the start of an HTML comment as the very first thing in the script and to ignore it in order that the HTML comment could be used to hide the script from people using Netscape 1, Mozaic, Internet Explorer 1, Internet Explorer 2, and other browsers of similar vintage none of which anyone uses any more. It is these prehistoric browsers (in JavaScript terms) that are meant when you see references in antiquated JavaScript tutorials to wrapping your JavaScript inside an HTML comment in order to hide it from "older" browsers.

使用Internet Explorer 3,Microsoft引入了他们自己的JavaScript,它们称之为JScript。从那时起,所有浏览器都至少识别出脚本标签和更现代的浏览器(Netscape 2 +,IE3 +等),不再需要HTML注释。所以当所有访问者都升级为使用Netscape 2,Internet Explorer 3或者比这两个中的任何一个更新的浏览器,脚本中的注释成为冗余代码。

With Internet Explorer 3, Microsoft introduced their own equivalent to JavaScript which they call JScript. Since then all browsers have at least recognised the script tag and more modern browsers (Netscape 2+, IE3+ etc) the HTML comment is no longer required.So once all your visitors have upgraded to use either Netscape 2, Internet Explorer 3, or a more recent browser than either of those two the commenting out of the script becomes redundant code.

这篇关于我应该使用HTML注释来封装JavaScript代码块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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