开发者

what does <!-- in Javascript do?

It's so hard to search for symbols in Google, so I ask here instead.

<!-- looks like a comment fo开发者_开发技巧r me, but it doesn't work like html. Or it's a one line comment just like // ?

What is the purpose and benefit of using this? Thanks

sample code :

<script type="text/javascript">
<!--
    alert("example");
//-->
</script>


It's an old method of hiding JavaScript from browsers that would treat the text node of a script element as normal text (and display your code).

Douglas Crockford recommends you don't use it anymore.

Do not use the <!-- //--> hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years. <!-- //--> is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include --, so a script that decrements has an HTML error.


It is HTML -- it's an HTML comment. Often used in a JavaScript block for CDATA: http://en.wikipedia.org/wiki/CDATA


JavaScript treats <!-- as a single line comment, the same as //. This is so you can wrap your JS code such that it looks like an HTML comment to browsers that don't understand JS:

<script>
    <!--
    alert('test');
    // -->
</script>


Comments in html

  • <!-- ... -->

Comments in javascript are like c:

  • One line comment = // ...
  • Multi line comment = /* ... */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜