Forum Moderators: open

Message Too Old, No Replies

<!-- Avoid using comments -->

...in your JavaScripts

         

DrDoc

10:56 pm on Mar 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Historically we were recommended to use HTML comments to hide JavaScript from "older" browsers. It could look something like this:
<script type="text/javascript">
<!--

...

//-->
</script>

<!--
<script type="text/javascript">

...

</script>
//-->

However, XML compliant browsers (version 6/7 or later) are allowed to silently remove any content within these comments. This might cause unexpected results!

Note that it doesn't matter whether your page follows HTML, XHTML, or XML - the problem is the same. Therefore, do not use comments in your JavaScript!

The same goes for CSS...


Reference: [w3.org]

C.4. Embedded Style Sheets and Scripts
Use external style sheets if your style sheet uses < or & or ]]> or --. Use external scripts if your script uses < or & or ]]> or --. Note that XML parsers are permitted to silently remove the contents of comments. Therefore, the historical practice of "hiding" scripts and style sheets within "comments" to make the documents backward compatible is likely to not work as expected in XML-based user agents.

tedster

12:18 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript 1.0 was introduced with Netscape 2.0 (March 1996) and IE3 (June 1996) - and the scripts originally needed to be hidden from earlier browser versions, which tended to print the script on the page because they couldn't understand it.

I don't know of any reason to continue with the comment tags. 1996 was a long time ago.

Reference:
History of JavaScript, jscript and ECMAScript [webmasterworld.com]

g1smd

12:24 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should be exporting the javascript to an external .js file, called using:
<script type="text/javascript" language="javascript" src="/path/file.js"></script>

That makes for a neater HTML page and allows easier updating as the file can be re-used across the whole site.

korkus2000

12:26 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think a lot of the problem are cut and paste scripts that still use this convention. It will be a debugging nightmare in the future. "It should work, there is no problem." Finally looking to find that the person still has the JS in comments. Makes me want to put all of my javascripts in a js file.

DrDoc

5:57 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is also old tutorials that still are surprisingly up to date (for HTML standards) but are hopelessly outdated as far as newer browsers goes...

Then, of course, you also have those "blockheaded Bracegirdles from Hardbottle" who teach Web development at Colleges or whatever, and they haven't really kept themselves up to date on browser changes... They think that they don't have to worry about XML/XHTML as long as they teach HTML - which is where they would be wrong.

keyplyr

7:51 am on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




You should be exporting the javascript to an external .js file, called using:
<script type="text/javascript" language="javascript" src="/path/file.js"></script>
- g1smd

I believe that should now be written as:

<script type="text/javascript" src="/path/file.js"></script>

g1smd

10:44 pm on Mar 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> old tutorials that still are surprisingly up to date <<

I have noticed that very few tutorials ever seem to mention adding the type="text/javascript" attribute to the <script> tag, so the result is that nearly every site using javascript fails to validate.