2

I am trying to learn JavaScript from a book. The first chapter of the book says to use the following format to support older browsers that don't support JS. What it actually does is simple, it uses HTML comment tag to hide script from browsers that don't support JS. My doubt here is this code works fine for me in all the browsers but is showing error in Aptana Studio 3. Now I understand that the error is due to Aptana considering "<" as a relational operator but how can I resolve this error?

<script>
    <!--
        //some JS code over here...
    //-->
</script>

Error(Syntax Error: Unexpected Token) coming at :

<!--
k19
  • 83
  • 3
  • 10
  • 1
    Don't bother with compatibility with browsers that don't support JS. Every browser supports JS – Indiana Kernick Jul 08 '15 at 03:33
  • 1
    Yeah, it sounds like this is an OLD book. Cool to know, but I doubt you will ever need to use that. You can play around with it elsewhere for fun. http://jsbin.com/vuneyeqani/1/edit?html,output – m59 Jul 08 '15 at 03:34
  • 2
    http://stackoverflow.com/questions/808816/are-html-comments-inside-script-tags-a-best-practice – epascarello Jul 08 '15 at 03:40
  • just don't worry about that. – astroanu Jul 08 '15 at 03:49
  • @HosseinMaktoobian edited his answer with a solution that may work, but then he deleted it :/ His solution was to use a js comment before the html comment tag like this: `// – m59 Jul 08 '15 at 03:58
  • @m59 I added his solution to my answer as well as adding an explanation as to why this is bad practice. – Domecraft Jul 08 '15 at 04:09
  • @m59: I tried the JS Bin link..Cool tool! Sadly the problem I am facing is on Aptana only and this too doesn't work. The solution you thought of was creative though. – k19 Jul 08 '15 at 05:41

2 Answers2

1

I'm aware that this does not answer your question directly, but the truth is that this simply does not need to be done. If a browser does not know how to interpret the JavaScript, almost all browsers will ignore the code anyway. Furthermore, adding the <!-- // --> can be dangerous as well for the following reasons, given by Matt Kruse:

  • Within XHTML documents, the source will actually be hidden from all browsers and rendered useless
  • It is not allowed within HTML comments, so any decrement operations in script are invalid

For a more detailed explanation, I recommend you check out this documentation about the best practices for JavaScript and this question that explains why using HTML comments in JavaScript is bad practice.

If for whatever reason you still want to show content to the user if they have JavaScript disabled (or can't run it because of an old browser), use a <noscript> tag

If you truly are deadset on commenting out your JavaScript then use this code snippet instead, which shouldn't give you the error:

//<!--

//-->

If you have any more questions feel free to ask.

Community
  • 1
  • 1
Domecraft
  • 1,645
  • 15
  • 26
-1

Every browser now supports JS. This trick was used to prevent the first generation browsers from showing JS code as plain text.

You may wanna take a look at this article.

Do not use the <!-- //--> hack with scripts. It was intended to prevent scripts from showing up as text on the first generation browsers Netscape 1 and Mosaic. It has not been necessary for many years. <!-- //--> is supposed to signal an HTML comment. Comments should be ignored, not compiled and executed. Also, HTML comments are not to include --, so a script that decrements has an HTML error.

Edit 1: If you still wanna use this trick in Aptana Studio 3 try commenting the first part too:

<script>
    //<--
    Code Goes Here...
    //--!>
</script>

I didn't test but solved the error in aptana

hmak.me
  • 3,770
  • 1
  • 20
  • 33