简体   繁体   English

为什么我需要在HTML中注释<script>标记?

[英]Why do I need to comment the <script> tag in HTML?

Most examples I've seen have scripts in a html page being enclosed by 我见过的大多数例子都在html页面中包含脚本

<!--
...
-->

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. 这只是一种向后兼容的黑客攻击 - 当脚本首次插入静态HTML页面时,大多数浏览器都无法支持它们。 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. 如果没有注释,它们将忽略<script>标记的语义(它们不理解),然后将脚本源发布到页面上。

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. 具有讽刺意味的是,解决方案本身就是一个黑客攻击--AFAIK,HTML规范的任何部分都没有说应该在注释内部解析脚本标记。 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. 当然,使用XHTML,注释是注释,因此完全一致的浏览器必须忽略您的脚本。

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. 所以基本上,除非你想支持真的, 真的很老的浏览器(在突破一些新的成本),它不再需要做到这一点。

Really old browsers that didn't understand the <script> tag might assume that it was a formatting tag it didn't understand. 真正老的浏览器不理解<script>标签可能会认为它是一个它不理解的格式化标签。 They would gracefully fail by rendering the contents of the tag (the script) inline in the page. 他们会通过在页面中内嵌呈现标记(脚本)的内容而优雅地失败。

By HTML-commenting out the script too those browsers will ignore the content rather than rendering it. 通过HTML注释掉脚本,这些浏览器将忽略内容而不是呈现它。

In practice I doubt any of those browsers are still in use and that you can probably get away without the comments nowadays. 在实践中,我怀疑这些浏览器是否仍在使用中,如果没有现在的评论,你可能会离开。

It's for some old browsers that were in use last century. 它适用于上个世纪使用的一些旧浏览器。

You no longer need them any more today, but using CDATA is considered good practice if you write XHTML. 您今天不再需要它们,但如果您编写XHTML,使用CDATA被认为是一种很好的做法。

It's still a problem nowadays if your page gets processed by a sanitation parser that sanitizes by only rewriting the <script> tag into <xscriptx> . 如果您的页面由卫生解析器处理,现在仍然是一个问题,该解析器仅通过将<script>标记重写为<xscriptx> If you leave in the comment tag, then if the script tag gets sanitized, then at least your javascript will still be hidden from the user by the comment tags. 如果你留在评论标签中,那么如果脚本标签被消毒了,那么至少你的javascript仍将被评论标签隐藏。 If you leave out the comments, the code will be visible. 如果省略注释,代码将可见。

An example of a sanitation parser is Google Translate, Google Cache or Proxomitron. 卫生解析器的一个示例是Google Translate,Google Cache或Proxomitron。

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

相关问题 我无法在HTML标记内插入注释的原因? - Reason why I can't insert a comment inside the HTML tag? 为什么我不需要在脚本标签中设置 onload 事件来运行 setInterval() 方法? - Why i do not need to set onload event to run setInterval() method in the script tag? 在脚本标记中执行一些javascript后,为什么在html文件中有此文本“ undefined”? - Why do I have this text “undefined” in my html file after I execute some javascript in script tag? 如果没有任何操作,是否需要使用HTML表单标签? - Do I need to use HTML form tag if there is no action? 如何更新HTML脚本标签中嵌入的数据? - How do I update data embedded in the HTML script tag? 如何通过以下方式将 JSX 导入 HTML 页面<script> tag? - How do I import JSX to an HTML page via the <script> tag? 如何将 HTML 脚本标签复制到外部 javascript? - How do I copy HTML script tag to external javascript? 如何调试HTML脚本标记中编写的JavaScript - How do I debug JavaScript written within a script tag in the HTML 为什么我们需要在从 chrome 扩展注入脚本后删除脚本标签? - Why do we need to remove script tag after injecting script from chrome extension? 如何添加<script> tag as a simple string without it being treated as a regular <script> tag in HTML? - How do I add a <script> tag as a simple string without it being treated as a regular <script> tag in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM