脚本标记中的HTML注释是否是最佳实践? [英] Are HTML comments inside script tags a best practice?

查看:109
本文介绍了脚本标记中的HTML注释是否是最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的练习在我必须使用的内联JavaScript中相当普遍:

 < script type =text / JavaScript的> 
<! -
// Code goes here
// - >
< / script>

我知道重点在于防止与JavaScript不兼容的浏览器渲染源代码,但是这仍然是今天的最佳做法?今天使用的绝大多数浏览器都可以解释JavaScript;即使是现代移动设备通常也没有问题。



关于'为什么不?'问题:我最近不得不花费几个小时来调试某人离开的问题关闭隐藏在某些页面的脚本标记末尾的' - >'前的'//',这导致了神秘的JavaScript错误。



你做什么?这仍然被认为是最佳做法。 解决方案

重要的是,现在,特定的浏览器是否支持JavaScript是无关紧要的(显然绝大多数都这样做) - 它是无关紧要的,因为几乎所有都理解脚本块,这意味着即使它们无法解释它,它们也会忽略JavaScript。



Matt Kruse对​​他的作品稍作更详细的解释 JavaScript Toolbox 网站为什么不在脚本块中使用HTML注释。



从该页面引用: / p>




不要在脚本块中使用HTML注释




在javascript(1995)的古老日子里,一些浏览器如Netscape 1.0没有任何脚本标记的支持或知识。所以当JavaScript第一次发布时,需要一种技术来隐藏旧版浏览器的代码,以便它们不会将其显示为页面中的文本。 'hack'是在脚本块中使用HTML注释来隐藏代码。



在脚本中使用HTML注释很差

  //不要这样做!代码只是代表如何完成任务
< script language =javascript>
<! -
//此处代码
// - >
< / script>




今天没有常用的浏览器不知道< script>标记,因此不再需要隐藏javascript源代码。事实上,这可能是有害的,原因如下:




  • 在XHTML文档中,源代码实际上将隐藏所有浏览器和呈现无用的
  • - 在HTML注释中都是不允许的,所以脚本中的任何递减操作都是无效的

The following practice is fairly commonplace in the inline JavaScript I have to work with:

<script type="text/javascript">
   <!--
       // Code goes here
   //-->
</script>

I know that the point is to prevent browsers that are incompatible with JavaScript from rendering the source, but is this still a best practice today? The vast majority of browsers used today can interpret JavaScript; even modern mobile devices usually don't have trouble.

As for the 'why not?' question: I recently had to spend several hours debugging an issue where someone had left off the '//' in front of a '-->' at the end of a script tag buried deep in some pages, and this was causing mysterious JavaScript errors.

What do you do? Is this still considered a 'best practice?'

解决方案

The important thing is that nowadays, whether a particular browser supports JavaScript or not is irrelevant (clearly the great majority do) - it is irrelevant because almost all understand script blocks, which means that they know to ignore the JavaScript even if they can't interpret it.

Matt Kruse gives a slightly more detailed explanation on his JavaScript Toolbox site for why specifically not to use HTML comments within script blocks.

Quoted from that page:


Don't Use HTML Comments In Script Blocks

In the ancient days of javascript (1995), some browsers like Netscape 1.0 didn't have any support or knowledge of the script tag. So when javascript was first released, a technique was needed to hide the code from older browsers so they wouldn't show it as text in the page. The 'hack' was to use HTML comments within the script block to hide the code.

Using HTML Comments In Script Is Bad

// DON'T do this! Code is just representative on how things were done
<script language="javascript">
<!--
   // code here
//-->
</script>

No browsers in common use today are ignorant of the <script> tag, so hiding of javascript source is no longer necessary. In fact, it can be considered harmful for the following reasons:

  • Within XHTML documents, the source will actually be hidden from all browsers and rendered useless
  • -- is not allowed within HTML comments, so any decrement operations in script are invalid

这篇关于脚本标记中的HTML注释是否是最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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