Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 591
Sufendy Avatar asked May 09 '11 04:05

Sufendy


1 Answers

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.

like image 54
alex Avatar answered Sep 25 '22 02:09

alex