admin管理员组

文章数量:1300041

How specifically does JavaScript understand the construct <!--? From the point of view of JavaScript, is it yet another ment in addition to // and /* */?

From testing it seems that JavaSript treats <!-- like //: a one-liner

<script> <!-- alert('hi') //--> </script>

does nothing, while

<script> <!-- 
alert('hi') //--> </script>

works as expected.

Where is this behavior documented?

This is not a duplicate of other questions: I don't ask why or whether or how it should be used. I ask what syntax and semantics it has in JavaScript, formally. The question is non-trivial and is not answered in other questions: for example, the behavior indicated above cannot be guessed from the other questions and their answers (in fact this was my motivation: my program with a one-liner as above did not work, and those questions and answers were no help in understanding why).

How specifically does JavaScript understand the construct <!--? From the point of view of JavaScript, is it yet another ment in addition to // and /* */?

From testing it seems that JavaSript treats <!-- like //: a one-liner

<script> <!-- alert('hi') //--> </script>

does nothing, while

<script> <!-- 
alert('hi') //--> </script>

works as expected.

Where is this behavior documented?

This is not a duplicate of other questions: I don't ask why or whether or how it should be used. I ask what syntax and semantics it has in JavaScript, formally. The question is non-trivial and is not answered in other questions: for example, the behavior indicated above cannot be guessed from the other questions and their answers (in fact this was my motivation: my program with a one-liner as above did not work, and those questions and answers were no help in understanding why).

Share Improve this question edited Mar 7, 2015 at 20:11 Alexander Gelbukh asked Mar 3, 2015 at 21:50 Alexander GelbukhAlexander Gelbukh 2,2401 gold badge19 silver badges29 bronze badges 4
  • 1 Newlines make a difference in JavaScript. alert('hi') alert('bye') is invalid while alert('hi')\nalert('bye') is okay. – Matthew Commented Mar 3, 2015 at 21:53
  • Duplicate is a duplicate of stackoverflow./questions/1507939/… – mplungjan Commented Mar 3, 2015 at 21:55
  • 2 The "duplicate" doesn't really answer this question. This question isn't "what are those HTML ments doing in my script tag, and do I need them?", it's "does (and why does) an HTML ment act as a single-line ment in Javascript?" – John Flatness Commented Mar 3, 2015 at 22:10
  • Thank you! I know newlines make a difference. What I did not know is that <!-- forces JavaScript to ignore the rest of the line. I wonder how many people have used a one-liner as I cite and didn't know it was not going to work. Other questions have nothing to do with this behavior. – Alexander Gelbukh Commented Mar 3, 2015 at 22:10
Add a ment  | 

1 Answer 1

Reset to default 12

From testing it seems that JavaSript treats <!-- like // a one-liner

Yes it does, as specified in the ES6 spec, annex B:

B.1.3 HTML-like Comments

Comment ::
    MultiLineComment
    SingleLineComment
    SingleLineHTMLOpenComment
    SingleLineHTMLCloseComment
    SingleLineDelimitedComment

SingleLineHTMLOpenComment ::
    <!-- SingleLineCommentCharsopt

However, note the description of annex B:

This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification. However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex defined the requirements for interoperable implementations of these legacy features.

These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.

So, this part only exists to describe existing "unofficial" behavior and as soon as browsers stop implementing this behavior, will be removed from the spec.

本文标签: Does HTML comment lt act as a singleline comment in JavaScriptand whyStack Overflow