Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Javascript still be "hidden" in HTML comment tags?

I have recently inherited some web code and found that all the Java Script scripts are contained within HTML comment tags

For example:

<script type="text/javascript" language="javascript"><!--
    function ValidateForm() { ... }//-->

As I understand it, this method prevented older, non-supported, browsers from interpreting your Java Script. However this is not something I was ever taught to do, and I wonder if this is now considered to be unnecessary, or is this still a common practice? If so, why?


Update: Thanks to kennebec for your advice to leave them in, which I have done for now, and thanks to Emmett also: I'll be sure to leave them out of any future code I write!

like image 279
Andy F Avatar asked Dec 12 '22 16:12

Andy F


1 Answers

http://javascript.crockford.com/script.html:

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.

like image 52
Emmett Avatar answered Jan 08 '23 17:01

Emmett