What's the point of wrapping javascript code in <!-- -->
?
The wrap() method wraps specified HTML element(s) around each selected element.
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.
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.
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.
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.
check
does javascript code need to start with "<!-- "?
now you don need to wrapping js code in <!-- -->
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With