简体   繁体   中英

what does <!— in Javascript do?

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

<!-- looks like a comment for 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.

Comments in html

  • <!-- ... -->

Comments in javascript are like c:

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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