5

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>  

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

6 Comments

great answer. much more concrete answer than mine!
but if we don't hide using // nothing worse happens. Code works as it should in firefox?
Browsers are really good at recovering from &^%$, but aren't always consistent in how they do it.
@sandbox: I guess it wasn't supported in old browsers. Although moderns browsers may allow it, the whole <!--///--> stuff is not meant for them.
Impressive as always. Your knowledge of the spec is incredible
|
4

-- is a JavaScript operator. It is used not to confuse the parser.

You really don't need those HTML comments anymore, BTW.

2 Comments

But why is --> in comments, and <!-- not? Is <!-- valid javascript then, and why (not)?
@Konerak - Quentin's answer is more complete.
1

This is a non-standard feature that browsers and JavaScript engines have always implemented. Nowadays, it cannot be removed, as that would break backwards compatibility. It’s detailed in the JavaScript / Web ECMAScript spec:

<!-- must be treated as the start of a SingleLineComment — equivalent to //.

var x = true;
<!-- x = false; // note: no syntax error
x; // true

--> at the start of a line, optionally preceded by whitespace or MultiLineComments, must be treated as a SingleLineComment — equivalent to //.

var x = true;
--> x = false; // note: no syntax error
x; // true

var x = 1;
/*
multiline comment!
x = 2;
*/ --> x = 3;
x; // 1

Comments

0

See HTML Comments and JavaScript

Comments

0

they are also use so that old versions of netscape dont through errors: http://www.yourhtmlsource.com/javascript/basicjavascript.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.