Do we still need <!-- //--> in JavaScript block [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using HTML comment tag <!— --> still relevant around JavaScript code?
Kind of remember <!-- //--> is used to prevent JavaScript code from being displayed in a lower version of IE. Anyone can provide a link to the article explaining this? Hard to search "<!--" in Google, because it's got stripped off.
And do we still need this in JavaScript block?
Thanks!

Those comments were used when Javascript was first introduced. They are not needed any more, and haven't been for quite a while.
The purpose was to hide the script from browsers that didn't even know what the script tag was for. Those browsers would ignore the script tag and show the code inside as regular HTML content.
There are no browsers left that don't know about the <script> tag, so even if they don't run the script, they still don't show the code inside.

Only if you're worried about time-travellers from somewhere back in last milennium coming to the present day with their old computers.

Related

Why would a <script> tag contains HTML comments in it? [duplicate]

This question already has answers here:
Are HTML comments inside script tags a best practice? [closed]
(10 answers)
Closed 2 years ago.
In this app I'm working on I see this pattern a lot:
<script type="text/javascript">
<!--
doStuff();
//-->
</script>
What purpose might those comment tags serve? Can I safely remove them?
In ancient times some browsers didn’t understand the <script> tag so it was made in a way that you can add comments to the beginning without issues. This causes anything inside to not be shown on the page if the browser doesn’t know what to do with the tag.
This hasn’t been needed in a very long time so it’s safe to remove them.

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.?
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
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.
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.
Those are comments, just removed if no documentation comments are there
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.

Why should I remove script tags from the DOM?

I just discovered a framework removing the script tags from the markup after the page was finished loading. I've tried searching for a "why", but all I discover is many people searching for a way to do it without saying why.
Is this a safety precaution?
Are there other tags that should be removed after as well?
As an example, here's a previous question that I asked about it just a little while ago:
Why are <script> tags not showing up in the inspector in Firefox on the todomvc example for AngularJS but are for the source?
That's originally where I discovered this.
I got one comment there that explained why he would do it in his case.
I don't know angularjs that well to tell why they remove the script that are not templates, could be a coding guideline. I myself remove them, because jquery once had an unexpected behavior that had the result that scripts where executed each time one of their ancestors where wraped. The bug is fixed but to avoid that this happens again I remove them.

Is there any difference between including JavaScript right before the </body> tag and right before the </html> tag? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it wrong to place the <script> tag after the </body> tag?
//scripts
</body>
</html>
or
</body>
//scripts
</html>
I know it's kind of a silly question but is there any difference between these two methods. I've read in books to do just before the </body> tag but in practice I've seen people do it just before the </html> tag. My instincts and current findings say that it shouldn't matter I don't think it would but being as I'm newer to web development I always second guess my work cause there could be something I don't know about.
The comments are correct: <script> outside the head or body are not valid HTML.
Here is the relevant spec
Content Model
A head element followed by a body element.
I'm not an expert, but in my experience, as long as your JS is in the header or body tag, it doesn't make a large difference where you place your JS.
I and my coworkers always place our JS in the header tag (for readability and consistency). But I've read (here and here) that to get the best performance you should place you JS just before your closing BODY tag. Again, in my experience I haven't noticed a major performance difference.

Why use "//-->" in javascript

I've seen this tag used after javascript functions for over a decade now and never asked WHY. It's seen in most of the tutorials I've seen over this time, but I usually leave it out... it doesn't seem to have an effect one way or another. Can anyone enlighten me as to why this is used?
If it's just to signify the end of a javascript function, wouldn't the right brace be adequate? If it's for a series of functions, the ending script tag is used.
I doubt I need an example, but for all the other readers out there that are also wondering what it's for, here's some code:
function helloWorld() {
document.write('Hello World');
}
//-->
Thanks in advance!
It's a holdover from the days when browsers without JS support were reasonably common. If you were including JS code directly in an HTML file, you'd include HTML comment tags (<!-- -->) around the code so those browsers didn't display it as part of the page. The reason for // --> is so that browsers that do support JS didn't try to interpret the closing HTML comment as part of the JS code.
Back in the days, some browsers did not handle javascript so to avoid errors, you'd put the javascript code inside an HTML comment block "".
Today, the XHTML standards says you should escape your script like
<script type="text/javascript">
<![CDATA[
... unescaped script content ...
]]>
</script>
You don't have to do that for HTML. Refer to:
http://www.w3.org/TR/xhtml1/
http://www.w3schools.com/tags/tag_script.asp
It's to comment out the code, so legacy browsers that don't support JS won't see it.
That's probably pretty much useless nowadays.
Note that it also needs a <!-- in the beginning to make it a comment.
Read this: http://lachy.id.au/log/2005/05/script-comments
If you notice, you can also usually see a <!-- before the scripts.
That syntax is used so browser that don't support Javascript will ignore the body of those scripts.
In fact, in html, anything surrounded by <!-- and --> is considered to be a comment
It's also useful if you're validating your html code, so the js doesn't appear as invalid html markup. As everyone mentioned however, it's fairly obsolete. I personally try never to have inline javascript, always in an external file that can be cached, which makes this style of coding useless
For browsers that do not understand JavaScript or if JavaScript is manually turned off these start <!-- and end html_comment_tags --> will help the bad_data inside the script > / ? < & ^ % # ) ! - ( to be commented out
If a browser understands javascripts it will not parse the last comment_ending tag--> due to the // placed before the html_comment_end tag--> and if the browser does not understand the meaning of // comment tags of javascript(simply because jscript is turned off) then it obviously parses the --> in the usual way and comments out everything inside the <script></script> including the //

Categories

Resources