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>
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With