Should Javascript still "hide" in HTML comment tags?

I recently inherited some web code and found that all Java Scripts are contained in HTML comment tags

For instance:

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

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


Update : thanks to kennebec for your advice to leave them what I did now, and thanks to Emmett as well: I will definitely leave them from future code I write!

+4
source share
4 answers

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

Do not use hack <!-- //--> using scripts. It was intended to prevent scripts displayed as text on first-generation browsers Netscape 1 and Mosaic. This has not been necessary for many years. <!-- //--> to signal HTML comment. Comments should be ignored, not compiled and executed. In addition, HTML comments do not include -, therefore, a script that decrements has an HTML error.

+11
source

This is due to XHTML authentication. HTML comments around js code should use an external tag. Presumably, the validator should look at your html, not your js.

I highly recommended this text https://developer.mozilla.org/en/properly_using_css_and_javascript_in_xhtml_documents where is all about this topic.

+2
source

My site is XHTML 1.1, which is XML.

My JavaScript is wrapped in //<![CDATA[ and //]]> if there is an XML object in the code (such as '&', '<' or '>').

0
source

Deleting <!-- may cause the script to crash if there is a </script as part of the program text inside <!-- ... --> .

For instance,

 <script><!-- function containsScriptEndTag(s) { return s.indexOf('</script>') >= 0; } //--> </script> 

will be sorted out differently without <!-- ... --> .

So go ahead and delete them, but check first.

Btw, inside script elements, they are not called “comments”, they are called “text scroll escaping”. From http://www.w3.org/TR/2009/WD-html5-20090423/syntax.html#syntax-escape

Passing text is an interval of text starting with text with text that does not by itself escape a space of text and ends at the next escape end of the text. There can be no references to characters inside the running text - sequences of characters that appear to be references to characters do not have special meaning.

Starting the initial text in the text is part of the text, which consists of a four-character sequence "

The end end of the text scroll is part of the text consisting of three sequence characters "→" (U + 002D HYPHEN-MINUS, U + 002D HYPHEN-MINUS, U + 003E MORE THAN A SIGN), whose U + 003E MORE THAN A SIGN (> )

The accelerated initial period of the text can be divided by its characters U + 002D HYPHEN-MINUS with the corresponding span end text.

0
source

Source: https://habr.com/ru/post/1334951/


All Articles