Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of <!-- and //--> in Javascript? [duplicate]

Possible Duplicate:
Why does this Javascript code inside a non-Javascript browser have extra commenting?

Sometimes I'm seeing some Javascript which begins with <!-- and ends with //-->.

I wonder what is the use of such line? As far as I know, <!-- means comments line. But that does not seem to be the case, as the script will still work even with such line. And it also works if the line is removed.

Here is one short example:

<script language='JavaScript' type='text/javascript'>
<!--
    function changelang(id) {
    document.location.href='index.php&lch=1&lang=' + id }
//-->
</script>
like image 286
deathlock Avatar asked Feb 21 '23 03:02

deathlock


2 Answers

It used to be used for old browsers which did not understand script tags and would render their contents nodes as text, which of course was undesirable.

This may make more sense when you think of contents of canvas, audio, noscript elements (the latter of which is treated special today, but imagine back in the old days where the browser did not know what the noscript element was).

In this day and age, it is not required.

Douglas Crockford recommends against their use.

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.

Source.

like image 121
alex Avatar answered Feb 23 '23 17:02

alex


This is an old convention used to hide the textual contents of scripts from browsers that didn't understand the script tag (versus having it disabled).

like image 21
Tim M. Avatar answered Feb 23 '23 16:02

Tim M.