最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

HTML Opening-Comment is valid JavaScript? - Stack Overflow

programmeradmin2浏览0评论

An old idiom for getting very old browsers to ignore JavaScript blocks in HTML pages is to wrap the contents of the <script> element in HTML ments:

<script>
    <!--
        alert("Your browser supports JavaScript");
    //-->
</script>

The rationale is that old JavaScriptless browsers will render as text the contents of the <script> element, so putting the JavaScript in an HTML ment makes the browser have nothing to render.

A modern browser, on the other hand, will see the <script> element and parse its contents as JavaScript. Consequently, the ments need to be valid JavaScript. The closing HTML ment (-->) is ignored by the JavaScript parser because it is preceded by a JavaScript line-ment (//).

My question is, how does the opening HTML ment (<!--) not cause the JavaScript parser to fail? I have heard from various people that the opening HTML ment is valid JavaScript. If it's true that the opening ment is evaluated as JavaScript, what does it do when it executes?

An old idiom for getting very old browsers to ignore JavaScript blocks in HTML pages is to wrap the contents of the <script> element in HTML ments:

<script>
    <!--
        alert("Your browser supports JavaScript");
    //-->
</script>

The rationale is that old JavaScriptless browsers will render as text the contents of the <script> element, so putting the JavaScript in an HTML ment makes the browser have nothing to render.

A modern browser, on the other hand, will see the <script> element and parse its contents as JavaScript. Consequently, the ments need to be valid JavaScript. The closing HTML ment (-->) is ignored by the JavaScript parser because it is preceded by a JavaScript line-ment (//).

My question is, how does the opening HTML ment (<!--) not cause the JavaScript parser to fail? I have heard from various people that the opening HTML ment is valid JavaScript. If it's true that the opening ment is evaluated as JavaScript, what does it do when it executes?

Share Improve this question asked Oct 27, 2012 at 9:08 DanielDaniel 10.2k3 gold badges48 silver badges62 bronze badges 5
  • It is valid, just insert it into your console. I'd be curious to know why, though. – kapa Commented Oct 27, 2012 at 9:11
  • 1 when executed it returns undefined, and works like a regular // ment. looks like its a hardcoded magic string ? curious to know the actual answer, as well. – c69 Commented Oct 27, 2012 at 9:17
  • Nice spot :) Will start doing line ments like this from now on just to play with persons minds :D – clentfort Commented Oct 27, 2012 at 9:22
  • 1 @c69: Looks like that's the best answer so far: well spotted! – Daniel Commented Oct 27, 2012 at 10:10
  • 1 @c69: According to javascripter Browsers support 3 types of ments: // slash-single line ment <!-- html-single line ment /* slash-star-multi line ment */ It is not in Standard ECMA-262. But I think it's obvious why browsers added it. – SublimeYe Commented Oct 29, 2012 at 8:38
Add a ment  | 

2 Answers 2

Reset to default 7

It seemed to be something exciting, an expression that might have a special meaning (<, ! and -- are all operators in Javascript), but without operands it does not make sense.

Turns out that <!-- is simply equivalent to // in Javascript, it is used to ment out one line.

It is a language feature that does not seem to be well-documented though, and might have been added for the simple reason to support this "hack". And now we have to live with it not to break backwards patibility.

Needless to say that while this is a funny thing to know, this type of menting should not be used in real code that other people might happen to read and work with.

The "hack" is also obsolete, because now every browser understands the <script> tag and does not display its contents (even if Javascript is turned off). Personally, in most cases I try avoid writing Javascript directly into HTML anyways and always load my Javascript as an external resource to separate HTML and Javascript.

In another StackOverflow question, @MathiasBynens gave what I believe is the answer:

Why is the HTML ment open block valid JavaScript?

In short, apparently, this is a non-standard addition to browser-based JS engines that allows these <!-- and --> as single-line ment markers like the // in standard JS.

发布评论

评论列表(0)

  1. 暂无评论