简体   繁体   中英

Is <!--// //--> needed in javascript anymore? What was it used for?

Is needed in javascript anymore? What was it used for?

Thanks

It is not needed any more. It was used to hide Javascript in old old browsers that didn't understand the <script> tag. Every browser made since the mid 90s understands Javascript, even if it doesn't support it for some reason. It would be //<!-- and //--> . The leading // comments that line for browsers that understand Javascript. Browsers that don't understand Javascript would see the body of the script tag wrapped in an HTML comment ( <!-- --> ), so they still wouldn't render it as if it was plain text.

It was only needed for Netscape 1 and Mosaic.

From Douglas Crockford:

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 hasn't been needed...basically ever.

I believe it is used to ensure that browsers that don't understand javascript won't show it as text. A super old browser will render

<script type="text/javascript">
    var something = 0;
</script>

Just like a modern browser would render

<span>
    var something = 0;
</span>

Which, of course, is rendered as text. The comments at the beginning/end tell the browser to treat the contents as an HTML comment. Modern browsers know to ignore those.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM