homepage Welcome to WebmasterWorld Guest from 54.211.34.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
<!-- Avoid using comments -->
...in your JavaScripts
DrDoc

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



 
Msg#: 319 posted 10:56 pm on Mar 20, 2003 (gmt 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.

 

tedster

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



 
Msg#: 319 posted 12:18 am on Mar 21, 2003 (gmt 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]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 319 posted 12:24 am on Mar 21, 2003 (gmt 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.

korkus2000

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



 
Msg#: 319 posted 12:26 am on Mar 21, 2003 (gmt 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.

DrDoc

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



 
Msg#: 319 posted 5:57 am on Mar 21, 2003 (gmt 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.

keyplyr

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



 
Msg#: 319 posted 7:51 am on Mar 21, 2003 (gmt 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>
- g1smd

I believe that should now be written as:

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

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 319 posted 10:44 pm on Mar 21, 2003 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved