Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping javascript in <!-- -->

Tags:

javascript

What's the point of wrapping javascript code in <!-- --> ?

like image 297
niaher Avatar asked May 31 '10 16:05

niaher


People also ask

What is wrap JS?

The wrap() method wraps specified HTML element(s) around each selected element.

How do you use wrap function?

wrap() is used to wrap a function inside other function. It means that the first calling function (a function which is calling another function in its body) is called and then the called function is being executed. If the calling function does not call the called function then the second function will not be executed.

What does the jQuery wrap () function do?

jQuery wrap() method is used to wrap specified HTML elements around each selected element. The wrap () function can accept any string or object that could be passed through the $() factory function.


4 Answers

This is one of my pet peeves. This is an ancient developer practice to "protect" older browsers that didn't understand <script> tags. Without it, they might fail to load the page or display the script content as html content.

But we're talking ancient browsers here. The only browsers that ever failed on parsing javascript were 1995 era browsers. These browsers are simply not in use today. This practice is just a holdover from 90's era web development.

No one should ever be putting <!-- --> blocks around their script tag content anymore.

If you see developers that are still doing this, please correct them. Not parsing javascript hasn't been an issue since Netscape 1.0, and this practice is actually considered harmful for modern browsers.

like image 92
womp Avatar answered Oct 22 '22 13:10

womp


By wrapping Javascript code in an HTML comment, you prevent older browsers that do not support Javascript from trying to treat the code as HTML.

like image 28
Mark Rushakoff Avatar answered Oct 22 '22 13:10

Mark Rushakoff


check

does javascript code need to start with "<!-- "?

now you don need to wrapping js code in <!-- -->

like image 32
Sungguk Lim Avatar answered Oct 22 '22 13:10

Sungguk Lim


It prevents user agents that are either not aware of the <script> tag or do not handle it properly from trying to parse or display JavaScript code as HTML.

It's common perception that you have to travel to the late 1990's to find a browser that's unaware of the <script> tag. However, my own sites are often spidered by tools that implement very rudimentary parsers (why not? You can write a grabber with 10 lines of PHP). And I've also found JavaScript code showing up in the middle of a document after pasting from a web site into a desktop app that's supposed to accept HTML from the clipboard. Thus, escaping non-HTML contents in HTML comments is not as silly as it may seem.

like image 38
Álvaro González Avatar answered Oct 22 '22 15:10

Álvaro González