简体   繁体   English

在JavaScript块上使用HTML注释仍然有意义吗?

[英]Does it still make sense to use HTML comments on blocks of JavaScript?

In the past people used to wrap HTML comment tags around blocks of JavaScript in order to prevent "older" browsers from displaying the script. 过去,人们习惯在JavaScript块周围包装HTML注释标签,以防止“旧”浏览器显示脚本。 Even Lynx is smart enough to ignore JavaScript, so why do some people keep doing this? 即使是Lynx也足够聪明,可以忽略JavaScript,那么为什么有些人会继续这样做呢? Are there any valid reasons these days? 这些天有没有正当理由?

<script type="text/javascript">
<!--
//some js code
//-->
</script>

Edit: There is ONE situation I did encounter. 编辑:我遇到过一种情况。 Some code editors, such as Dreamweaver, get confused by quoted HTML inside a JavaScript string when in "design view" and try to display it as part of your page. 某些代码编辑器(如Dreamweaver)在“设计视图”中被JavaScript字符串中的引用HTML弄糊涂,并尝试将其显示为页面的一部分。

No, absolutely not. 不,绝对不是。 Any user agent, search engine spider, or absolutely anything else these days is smart enough to ignore Javascript if it can't execute it. 现在任何用户代理,搜索引擎蜘蛛或其他任何其他东西都足够智能,如果无法执行它,就会忽略Javascript。

There was only a very brief period when this was at all helpful, and it was around 1996. 只有一个非常短暂的时期,这一点都很有帮助,而且大约在1996年。

There isn't a good reason to do this anymore, as the browsers which required this have by and large disappeared from the web. 没有充分的理由再这样做,因为需要它的浏览器基本上从网络上消失了。

In fact, doing this can actually cause unintended problems with certain older browsers' attempts to interpret the page if it uses XHTML - from developer.mozilla.org : 实际上,这样做实际上可能会导致某些旧浏览器尝试解释页面的意外问题,如果它使用XHTML - 来自developer.mozilla.org

  • Mozilla 1.1+/Opera 7 Mozilla 1.1 + / Opera 7

    Do not apply CSS or execute the JavaScript. 不要应用CSS或执行JavaScript。

  • Netscape 7.0x/Mozilla 1.0.x Netscape 7.0x / Mozilla 1.0.x

    Do not apply CSS but does execute the JavaScript. 不要应用CSS但执行JavaScript。

  • Internet Explorer 5.5+ Internet Explorer 5.5+

    Can not display the document. 无法显示文档。

That site also links to examples of the several problems mentioned above. 该网站还链接到上述几个 问题的例子。

You should use CDATA though... 你应该使用CDATA ...

<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */

/* ]]> */
</script>

Because if you have '<', '>', '&', etc in your code, the code won't validate :) 因为如果你的代码中有'<','>','&'等,代码将无法验证:)

Hell no, nobody needs this anymore and if you do, you have some more problems to care about. 不,不,没有人需要这个,如果你这样做,你还有一些问题值得关注。 When you really want to support browsers that need that, you have to watch out for a lot more things. 当你真的想要支持那些需要它的浏览器时,你必须要注意更多的东西。 Not even talking about the lack of css! 甚至没有谈论缺乏CSS!

However, the bigger problem is, that people do this wrong. 然而,更大的问题是,人们做错了。 Actually your example is wrong, because the line 其实你的例子是错的,因为这一行

-->

should read 应该读

//-->

secondly, you type attribute says "text/JavaScript" what is wrong too. 其次,你输入属性说“text / JavaScript”也有什么不对。 It has been "text/javascript" (all lower case) but this is obsolete (see the IANA List ) and now it should be "application/javascript" (see another IANA List . However, Douglas Crockford, the JS Guru, said you just should leave it out. 它一直是“text / javascript”(全部小写),但这已经过时了(参见IANA列表 ),现在它应该是“application / javascript”(参见另一个IANA列表 。但是,JS Guru的Douglas Crockford说你只是应该把它留下来。

Another Problem nobody mentioned already is this: Within HTML comments, "--" is not allowed and that means you can't use "x--" to decrement x by one. 没有人提到的另一个问题是:在HTML注释中,“ - ”是不允许的,这意味着你不能使用“x--”将x减1。

Not having to use CDATA blocks is one of the reasons I prefer to use HTML 4.01 Strict as my docttype, but, Staicu, I thought it used the following syntax: 不必使用CDATA块是我更喜欢使用HTML 4.01 Strict作为我的docttype的原因之一,但是,Staicu,我认为它使用了以下语法:

<script charset="utf-8">
//<![CDATA[

//]]>
</script>

Maybe the two are equivalent? 也许两者是等价的? Anyone know if there is an advantage to one over the other? 任何人都知道一个人有优势吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在JavaScript中使用===进行字符串比较是否有意义? - Does it make sense to use === for string comparison in JavaScript? 我应该使用HTML注释来封装JavaScript代码块吗? - Should I use HTML comments to capsulate JavaScript code blocks? 什么时候在 Javascript 中使用双重相等(松散相等)有意义? - When does it make sense to use double equality (loose equality) in Javascript? 在前端 javascript 应用程序中使用 DI 容器是否有意义 - Does it make sense to use DI container in front end javascript app 在学习Javascript时,软件开发人员使用jQuery是否有意义? - Does it make sense for software developer to use jQuery when learning Javascript? 将Web Workers用于游戏是否有意义? - Does it make sense to use Web Workers for a game? 在$ watch回调中使用$ scope是否有意义? - Does it make sense to use $scope into a a $watch callback? 我在一些遗留代码中看到了几个 catch 块。 这有任何意义吗? - I've seen several catch blocks in some legacy code. Does that make any sense? 仅在使用了var语句的块内使用它分配值,才有意义吗? - Does it make sense to assign a value to a variable declared with var statement only inside those blocks where it is used? 尝试协助JavaScript垃圾收集器是否有意义? - Does it make sense to attempt to assist the JavaScript Garbage Collector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM