为什么我需要评论< script>标记在HTML? [英] Why do I need to comment the <script> tag in HTML?

查看:127
本文介绍了为什么我需要评论< script>标记在HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的大多数示例都包含在HTML页面中的脚本

 <! -  
...
- >

我已经尝试写没有注释标签,似乎没有任何区别。为什么使用注释标签和它的功能是什么?

解决方案

这不是真的有必要了。这只是一种向后兼容性的攻击 - 当脚本首次被插入静态HTML页面时,大多数浏览器都不支持它们。没有注释,他们会忽略< script> 标签的语义,这是他们不明白的,然后将脚本源发送到页面上。 / p>

讽刺的是,这个解决方案本身就是一个黑客 - AFAIK,没有HTML规范的一部分说脚本标签应该在评论内部被解析。事实上,所有浏览器都选择了这一点似乎更像是一个巧合比任何其他。当然,使用XHTML时,评论是注释,因此完全一致的浏览器可以忽略您的脚本。



基本上,除非你想支持真的,真的旧浏览器(以破坏一些新的浏览器为代价),就不再需要这样做了。


Most examples I've seen have scripts in a html page being enclosed by

<!--
...
-->

I've tried writing it without the comment tags and there doesn't seem to be any difference. Why is the comment tag used and what function does it serve?

解决方案

It's not really necessary any more. This has only ever served as a backwards-compatibility hack of sorts - when scripts first started being inserted into static HTML pages, most browsers couldn't support them. Without the comments, they would ignore the semantics of the <script> tag (which they didn't understand), and then would emit the script source onto the page.

Ironically, the solution was a hack in itself - AFAIK, no part of the HTML spec says that script tags should be parsed when inside of comments. The fact that all browsers picked this up seems to be more of a coincidence than anything else. Certainly with XHTML, comments are comments so a fully conformant browser would have to ignore your scripts.

So basically, unless you want to support really, really old browsers (at the cost of breaking some new ones) it's no longer necessary to do this.

这篇关于为什么我需要评论&lt; script&gt;标记在HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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