Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting in JavaScript

What is the purpose of using // in the following code. If old browsers doesnt support javascript then the symbols <!-- --> will ignore js code. In case browsers support JS, these symbols <!-- --> will be ignored. Then wats the use of // symbols.

<html>
<body>
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
//-->
</script>
</body>
</html>  
like image 480
sandbox Avatar asked Feb 08 '12 15:02

sandbox


1 Answers

If old browsers doesnt support javascript then the symbols <!-- --> will ignore js code.

True, assuming HTML and for a definition of "old browsers" equal to "Netscape 1 era". Don't use them today.

In case browsers support JS, these symbols <!-- --> will be ignored.

Only half true. Only the start of the comment is special cased. From the specification:

The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line. JavaScript interprets "//" as starting a comment extending to the end of the current line. This is needed to hide the string "-->" from the JavaScript parser.

like image 63
Quentin Avatar answered Sep 30 '22 07:09

Quentin