Saif
Saif

Reputation: 7052

Necessity of <!-- code here //--> block in old jsp file

I am working on some pretty old code and in every jsp file where there is a javaScript block I see some bizarre Syntax. It goes like :

<script language="JavaScript">
<!--

    here is the main code (mixed jsp and javaScript)

//-->
</script>
neccesity <!--   code here   //-->

I don't find any necessity for <!-- //--> this syntax. But to remove these from the whole project I really have to be sure why these syntax was used.

Does those had any significance previously, I mean for browser issue or something?
Or,
Just a simple mistake which were carried on.?

Upvotes: 2

Views: 88

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1075079

Does those had any significance previously, I mean for browser issue or something?

Yes, it used to. Remember that a script tag embeds non-HTML inside an HTML document, in this case JavaScript code. Since the content of the script tag isn't HTML, but is being parsed by an HTML parser (while it looks for the ending tag), the idea was to use an HTML comment (<!-- ... -->) to contain the JavaScript code, so the HTML parser wouldn't get confused by HTML-like things within the JavaScript code, and so really old browsers that didn't understand script tags at all wouldn't show the "text" inside them.

The one you've quoted is one of many variations.

There's no need, at all, to do this in an HTML document, with modern browsers — or even with really old browsers, at this point.

In an XHTML document, you do need to do something, but what's shown there is likely to be insufficient; you'd need a CDATA section instead.

Assuming you're writing HTML5 documents (<!doctype html>), you can remove those comments from the JSPs.

Upvotes: 2

Arsen
Arsen

Reputation: 664

As stated here: http://www.w3schools.com/tags/tag_comment.asp

You can also use the comment tag to "hide" scripts from browsers without support for scripts (so they don't show them as plain text)

Nowadays not really an issue I suppose. But may not be the best idea to get rid of them.

Upvotes: 1

Kayaman
Kayaman

Reputation: 73568

Back in the days there were all sorts of tricks used to make sure that the content looked decent even if the user had javascript or cookies disabled.

This looks like it was meant for hiding the javascript, if the browser didn't understand Javascript.

Upvotes: 0

Jaromanda X
Jaromanda X

Reputation: 1

HTML style comments in javascript date back to a time when not all browsers supported javascript, so would render the code as text. The <script> tag was just assumed to be just like any other unknown tag, so the tag itself was not rendered, but the contents were

I don't think any current browser would have a problem with it, but I would recommend getting rid of it, because who knows what the future will bring

Upvotes: 3

Those are comments, just removed if no documentation comments are there

Upvotes: 0

Related Questions