tags:

views:

239

answers:

6

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

+7  A: 

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.

Mark Rushakoff
-1 ...but those are only ancient browsers. See womp's answer. This so did not deserve that many upvotes...
Vuntic
womp's answer is certainly the correct one. Good practice or not, compatibility with old browsers is why people still comment out their Javascript.
Mark Rushakoff
I agree with Mark. There's still a small population using those oldish browsers >_> Why? God knows.
ItzWarty
A: 

Older browser that don't understand Javascript will just see an HTML comment.

WDuffy
+1  A: 

check

http://stackoverflow.com/questions/2461353/does-javascript-code-need-to-start-with

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

sunglim
+18  A: 
womp
+1 the link you gave us is really nice for newbie
sunglim
You should take a look at some server logs. This month, I've had 27 visits from brosers identifying as Netscape 0.91 and 17 as Netscape 0.6. Sure, the agent string can be manipulated arbitrarily, but I can't see why anyone would deliberately fake those versions.
Michael Borgwardt
I don't feel that's meaningful. What percentage of your overall traffic is that? Are you really going to strive to be the *one* website that works for Netscape 0.6? You'll have to do a lot more than just comment your script tags.... I would bet that they are not legimate users at all, but either people doing research, or as you mentioned, using faked browser strings.
womp
I'm very curious about Michael's server logs as well. Is it actually possible that there are some users savvy enough to install a browser back when "Information Superhighway" was still an in vogue term, and yet not savvy enough to upgrade it in the intervening 14 to 15 years?! womp is totally right though. There's no reason we should be programming towards those users today ... whatever their story.
Greg Charles
@Michael - any browser that doesn't understand script tags isn't going to understand CSS, HTML 4, XHTML, Flash, PNG files, or any other relatively modern web technology. If you're going to support the 27 visitors who might be using Netscape 0.91, then you'd better have a non-CSS version of your site.
zombat
@Greg, @zombat: Oh, I'm not proposing that you should make a big effort to support those browsers (yes, they constitute something like 0.05% of the visitors), just that it's not necessarily correct to claim that they're "simply not in use today". And I'd love to find out more as well, but sadly, the agent strings are all I have. Hm, actually I could at least see whether there was OS information as well for these visitors...
Michael Borgwardt
I suspect it's just curiosity. I've run old browsers before to see what the modern web looked like in them.
Joeri Sebrechts
You'll have to tell Google Adsense then, I just noticed yesterday that they still use this.
DisgruntledGoat
+1  A: 

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.

Álvaro G. Vicario
A: 

Apart from some browsers possibly not supporting it there is another reason: the W3C XHTML validator (http://validator.w3.org) interprets JavaScript that is not in comment tags as if it were HTML. So something like "i < 0" will mean your website doesn't validate unless you use these tags.

Kees Kist