views:

729

answers:

11

The following practice is fairly commonplace in the inline JavaScript I have to work with:

<script type="text/javascript">
   <!--
       // Code goes here
   //-->
</script>

I know that the point is to prevent browsers that are incompatible with JavaScript from rendering the source, but is this still a best practice today? The vast majority of browsers used today can interpret JavaScript; even modern mobile devices usually don't have trouble.

As for the 'why not?' question: I recently had to spend several hours debugging an issue where someone had left off the '//' in front of a '-->' at the end of a script tag buried deep in some pages, and this was causing mysterious JavaScript errors.

What do you do? Is this still considered a 'best practice?'

A: 

The best practice is using xslt and comments in it.

kingoleg
-1 Why is XSLT the best practice for this situation?
John M Gant
Because any XML-based technology is clearly superior to anything else. Also, your sarcasm detector is clearly on the fritz.
GalacticCowboy
+8  A: 

I've stopped doing it. At some point you just have to let go of your NCSA Mosaic.

chaos
+1  A: 

I stopped doing that ages ago. You really don't need it in this day and age.

John Topley
A: 

I don't do it but the other day I went to validate my password protected site at w3c. So I had to use their direct input method. It complained about my javascript, so I put the comments back in everything was fine.

JoshBerke
A: 

If you are typing manually, I suggest you always use external js files, that would help so much.

Regarding your concern: most browsers are JavaScript safe today. However sometimes people may write simple parsers to fetch a HTML directly - and I must say, the safe quote is really helpful for those clients. Also some non-JS clients like old Lynx would get benefits from this.

Francis
+32  A: 
Noldorin
Excellent information, thank you very much!
tehblanx
I didn't know that, thanks
marcgg
+1. I knew there was someone who had a lot of information about this topic from my comp.lang.javascript days, I was perusing news group archives when your answer appeared, quoting Matt.
Grant Wagner
+4  A: 

No, it is a hangover from a workaround used when the script element was first introduced. No browser fails to understand the script element today (even if it understands it as "Script that should be ignored because scripting is turned off or unsupported").

In XHTML, they are actively harmful.

I wrote something about the history of it a while back.

David Dorward
+1  A: 

Stopped using this a while back. Also, according to Douglas Crockford, you can drop the type attribute from your script tags since the only scripting language available in most browsers is JavaScript.

jakemcgraw
Most browsers will still work, it violates the HTML specification though, so I wouldn't advise it.
David Dorward
you never know when will IE take VB script as default type..
Francis
+1  A: 

As per W3C Recommendation it was mainly useful to hide the script data from USER AGENTS.

Quoted from the W3c page :

Commenting scripts in JavaScript The JavaScript engine allows the string "<!--" to occur at the start of a SCRIPT element, and ignores further characters until the end of the line. JavaScript interprets "//" as starting a comment extending to the end of the current line. This is needed to hide the string "-->" from the JavaScript parser.

    <SCRIPT type="text/javascript">
<!--  to hide script contents from old browsers
  function square(i) {
    document.write("The call passed ", i ," to the function.","<BR>")
    return i * i
  }
  document.write("The function returned ",square(5),".")
// end hiding contents from old browsers  -->
</SCRIPT>
Webrsk
If HTML 4.x is being used. It is quite a different story with XHTML.
David Dorward
A: 

If you do not include literal text between script tags- that is, if you load scripts from src files, you can forget about the comments.

kennebec
A: 

I would recommend using a CDATA section, as described in this question.

Mike Harder