On the W3 tutorial, it shows htis code:
<html>
<body>
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
//-->
</script>
</body>
</html>
Then it says:
The two forward slashes at the end of ment line (//) is the JavaScript ment symbol. This prevents JavaScript from executing the --> tag.
This doesn't make sense to me. I thought the whole thing got mented out.
On the W3 tutorial, it shows htis code:
<html>
<body>
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
//-->
</script>
</body>
</html>
Then it says:
The two forward slashes at the end of ment line (//) is the JavaScript ment symbol. This prevents JavaScript from executing the --> tag.
This doesn't make sense to me. I thought the whole thing got mented out.
Share Improve this question edited Feb 4, 2013 at 18:52 Bill the Lizard 406k212 gold badges574 silver badges892 bronze badges asked Mar 14, 2012 at 20:55 CaffeinatedCaffeinated 12.5k41 gold badges128 silver badges225 bronze badges 2- 3 That is a very old legacy snippet. It only mattered on browsers that did not support the <script> element, even to just ignore it, and was there to "keep them from showing the JavaScript". – user166390 Commented Mar 14, 2012 at 21:03
- @pst - Yes, I will keep in mind that W3 tutorials may have some older and less-relevant material, thanks – Caffeinated Commented Mar 14, 2012 at 21:04
4 Answers
Reset to default 6In browsers that do understand JavaScript the opening <--
html ment is ignored and the JS code is executed. The JS ment //
on the last line then prevents the closing -->
being taken as an error by the JS engine. In browsers that don't understand JavaScript everything between <--
and -->
is taken as an html ment and ignored.
This whole thing was a precaution for older browsers that didn't know about JS. It is not necessary for any modern browser.
If you want to ment out a block of JS enclose the block in /*
and */
.
First of all, W3Schools has nothing to do with W3. Their tutorials were pretty horrible before people started plaining and their confusing name implies that they are somehow connected to W3, but in reality they aren't.
Second of all, this method is not needed anymore. There are no used browsers that don't support JS cleanly (links
, lynx
, etc. have no troubles with JS code whatsoever).
That being said, the code is supposed to do this:
<!--
I am a HTML ment
-->
<!--
If I am placed in a JS block, the web browser should ignore me
alert('and me');
-->
<!--
If you ment out the HTML ment ending tag, apparently
the browser will treat the ment as JS code *only*
if the browser supports JS.
//-->
If you make it like this, you'll get syntax error:
<script type="text/javascript">
<!--
document.getElementById("demo").innerHTML=Date();
-->
</script>
JavaScript doesn't know HTML's ment closing -->
, so it has to be mented out of script.
There is no need to use HTML-ments to separate JavaScript, except if you're using a simple text editor which colours the code (NoteTab etc.).
It does all get mented out.
In a bowser without Javascript everything between <!--
and -->
will be mented out.
Try to think of it this way: If you tried this code
<script type="text/javascript">
-->
</script>
then Javascript would throw an error.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745173857a4615089.html
评论列表(0条)