1

I have the joyous task of working on an old web application. In several html files, inline javascript is used with the following syntax:

<body>

html goes here...

    <script>
    <!--
        javascript goes here
    -->
    </script>
</body>

(EDIT: the script tags are inside the body)

Notice the html comment that is wrapped around the entire javascript code. This doesn't really do anything, as all browsers ignore html comments inside script tags. My question is simple: why did a developer from the past ever do this? Was this some kind of optimization or cross browser issue that could be fixed with a html comment "hack"?

I've removed the comments for now and everything looks to work fine.

user1884155
  • 3,616
  • 4
  • 55
  • 108

3 Answers3

2

Assuming your <script> element is inside the body and not after, this was a solution to let very old browsers unaware of what is a <script> element ignore its content instead of displaying it (browsers are supposed to display as text the content of tags they don't understand).

This hack should have disappeared very soon, as it was very rare to find browsers not knowing what a <script> element was but unfortunately this bad practices didn't die fast enough due to some bad tutorials still advising such constructs even recently.

Note that a script element outside of both the body and the head is invalid.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

In the ancient days of javascript (1995), some browsers like Netscape 1.0 didn't have any support or knowledge of the script tag. So when javascript was first released, a technique was needed to hide the code from older browsers so they wouldn't show it as text in the page. The 'hack' was to use HTML comments within the script block to hide the code. For detailed description check this link

source

Community
  • 1
  • 1
Pandiyan Cool
  • 6,381
  • 8
  • 51
  • 87
1

The inline HTML comments are to prevent older browsers that do not understand the script element from displaying the script code in plain text.

Older browsers that do not understand the script element will interpret the entire content of the script element above as one single HTML comment, beginning with "", effectively ignoring the script completely.

If the HTML comment was not there, the entire script would be displayed in plain text to the user by these browsers

Source

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60