12

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 comments:

<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 comment 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 comments need to be valid JavaScript. The closing HTML comment (-->) is ignored by the JavaScript parser because it is preceded by a JavaScript line-comment (//).

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

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

2 Answers 2

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 comment 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 compatibility.

Needless to say that while this is a funny thing to know, this type of commenting 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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Why is the HTML comment 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 comment markers like the // in standard JS.

1 Comment

I didn't realise the closing (HTML) comment is also recoginsed as a JavaScript comment too. It checks out (using the JS console in Firefox), but I wonder why people still use //-->. Maybe --> is only recently interpreted as a JS comment?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.