Welcome to WebmasterWorld Guest from 54.161.53.213

Forum Moderators: open

Message Too Old, No Replies

<!-- Avoid using comments -->

...in your JavaScripts

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

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


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.
12:18 am on Mar 21, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


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]

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 20, 2002
posts:3732
votes: 0


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.
5:57 am on Mar 21, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


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.

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

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8975
votes: 409



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>

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

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


>> 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.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members