简体   繁体   English

javascript 块中的 HTML 注释?

[英]HTML comments in a javascript block?

I have a function like this one (below) which inserts a block of HTML code in an HTML page:我有一个这样的函数(如下),它在 HTML 页面中插入一段 HTML 代码:

function someEventHandler(htmlContent)
{
  document.getElementById('some-element-id').innerHTML = htmlContent;
}

This works fine for HTML code that includes an img tag.这适用于包含img标签的 HTML 代码。

When the HTML code includes <script> blocks, though, they do not render.但是,当 HTML 代码包含<script>块时,它们不会呈现。 Furthermore, the script blocks contain JavaScript that is surrounded by HTML comments.此外,脚本块包含被 HTML 注释包围的 JavaScript。 For example:例如:

<script type="text/javascript">
<!--
    function someFunctionThatRendersStaticImageOrFlash()...
-->
</script>

These script blocks that contain HTML comments do not render after they have been inserted.这些包含 HTML 注释的脚本块在插入后不会呈现。 I have control over the code that is inserted and I tested it without the HTML comments, and they rendered successfully.我可以控制插入的代码,并且在没有 HTML 注释的情况下对其进行了测试,并且它们成功呈现。

It is my understanding that HTML comments like this were used in early versions of Netscape, to prevent problems with browser incompatibility with javascript.我的理解是在 Netscape 的早期版本中使用了这样的 HTML 注释,以防止浏览器与 javascript 不兼容的问题。

Is there any other reason (or any good reason) to include HTML style comments in a javascript block?是否有任何其他原因(或任何充分的理由)在 javascript 块中包含 HTML 样式注释?

Edit I mistyped.编辑我打错了。 The closing comment is: //--> .结束注释是: //-->

Actually, only <!-- is valid javascript.实际上,只有<!--是有效的 javascript。 To end the html comment in javascript, you actually need to use //--> .要在 javascript 中结束 html 注释,您实际上需要使用//-->

These days it's not really required to use the HTML comments in Javascript anymore.现在已经不再需要在 Javascript 中使用 HTML 注释了。 Browsers which are so old as to not understand the <script> tag are highly unlikely to be able to render much of anything on a "modern" website, and you should not have to accommodate users who are quite literally on a stone-age browser.太老以至于无法理解<script>标签的浏览器极不可能在“现代”网站上呈现大部分内容,而且您不应该适应那些完全使用石器时代浏览器的用户.

However, if you're intending to include your HTML inside an xml document, and/or are writing x-html, then you'll have to use the xml <![CDATA[ and ]]> enclosures, as various Javascript operators ( & , < , and > in particular) will cause XML parse errors without the enclosures.但是,如果您打算将 HTML 包含在 xml 文档中,和/或正在编写 x-html,那么您必须使用 xml <![CDATA[]]>附件,作为各种 Javascript 运算符( &<>特别是)将导致没有附件的 XML 解析错误。

This is a previously non-standard feature that browsers and JavaScript engines have always implemented.这是浏览器和 JavaScript 引擎一直实现的先前非标准功能。 Nowadays, it cannot be removed from the Web platform, as that would break backwards compatibility.如今,它不能从 Web 平台中删除,因为这会破坏向后兼容性。 It's detailed in the JavaScript / Web ECMAScript spec :JavaScript / Web ECMAScript 规范中有详细说明

<!-- must be treated as the start of a SingleLineComment — equivalent to // . <!--必须被视为SingleLineComment的开始——相当于//

 var x = true; <!-- x = false; // note: no syntax error x; // true

--> at the start of a line, optionally preceded by whitespace or MultiLineComment s, must be treated as a SingleLineComment — equivalent to // . -->在一行的开头,可以选择前面有空格或MultiLineComment s,必须被视为SingleLineComment — 等效于//

 var x = true; --> x = false; // note: no syntax error x; // true var x = 1; /* multiline comment! x = 2; */ --> x = 3; x; // 1

Update: This is now upstreamed to the ECMAScript spec .更新:这现在是 ECMAScript 规范的上游 For more background info, see Sunsetting the JavaScript Standard .有关更多背景信息,请参阅取消 JavaScript 标准

No, there is no reason to include them.不,没有理由将它们包括在内。 I regularly omit those comments and have no problems with modern browsers.我经常忽略这些评论,并且对现代浏览器没有任何问题。

<script> inside strings set via innerHTML never runs (primarily because it never has in IE and then sites depended on that, so when other browsers reverse-engineered the innerHTML implementation they had to do that too).通过innerHTML设置的字符串内的<script>永远不会运行(主要是因为它从未在IE 中运行,然后站点依赖于它,所以当其他浏览器对innerHTML实现进行逆向工程时,他们也必须这样做)。 The HTML comments there are a red herring.那里的 HTML 注释是一个红鲱鱼。

If you need the <script> to run, you need to use some other method of inserting the nodes (eg createContextualFragment ).如果您需要运行<script> ,则需要使用其他一些插入节点的方法(例如createContextualFragment )。

HTML comments in script blocks have not been necessary for many years.脚本块中的 HTML 注释已经很多年没有必要了。 Omitting them won't cause any problems.省略它们不会造成任何问题。

References: http://javascript.crockford.com/script.html参考资料: http : //javascript.crockford.com/script.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM