1. Computing

HTML Comments and JavaScript

Join the Discussion

Questions? Comments?

The general rule regarding HTML tags that browsers do not understand is that the browser should ignore the tag completely and treat the content of the page as if that tag were not there. This means that when Netscape 2 first introduced JavaScript (or LiveScript as it was called then), it was necessary to place an HTML comment around the actual script in order to hide the code from other browsers that didn't understand the script tag and which therefore would have displayed the code rather than running it.

<script language="livescript">
<!--
... script code goes here ...
// -->
</script>

The JavaScript language was specifically written to accept the start of an HTML comment as the very first thing in the script and to ignore it in order that the HTML comment could be used to hide the script from people using Netscape 1, Mozaic, Internet Explorer 1, Internet Explorer 2, and other browsers of similar vintage none of which anyone uses any more. It is these prehistoric browsers (in JavaScript terms) that are meant when you see references in antiquated JavaScript tutorials to wrapping your JavaScript inside an HTML comment in order to hide it from "older" browsers.

With Internet Explorer 3, Microsoft introduced their own equivalent to JavaScript which they call JScript. Since then all browsers have at least recognised the script tag and more modern browsers (Netscape 2+, IE3+ etc) the HTML comment is no longer required.So once all your visitors have upgraded to use either Netscape 2, Internet Explorer 3, or a more recent browser than either of those two the commenting out of the script becomes redundant code. In fact the only effect of that code in the more modern browsers is that if you use a content management system or use XHTML instead of HTML, you effectively convert the script into a comment by enclosing it in that tag.

Both JavaScript and JScript are now far more similar to one another than they originally were since both are now based on the same ECMAscript standard. Both languages therefore support the same basic code with the main differences being the way that event listeners are attached to your page and all of the extra commands that JScript supports which JavaScript doesn't (such as interfacing to ActiveX). Feature sensing easily allows us to handle these differences between JavaScript and JScript as well as allowing for slightly older browsers that dont't support all the commmands supported by the latest browsers.

With modern browsers where you serve your web page as HTML you should now code your script as follows in order to prevent the script being treated as a comment if you decide to start using a CMS and in order to remove unneeded code that just makes the page bigger than it needs to be.

<script type="text/javascript">
... script code goes here ...
</script>

The correct type to use for JavaScript is actually application/x-javascript however that type is specific to JavaScript and will not work for browsers that use JScript instead. The text/javascript type while technically obsolete does actually work with all current browsers to process the code as either JavaScript or JScript (whichever the browser supports).

If you are going to serve your pages as XHTML then a slight modification to the code is required. There are two types of content in a web page - CDATA and PCDATA. The difference between these two is that PCDATA is scanned in order to process any HTML tags and entities found in the content (things starting with < and &) while PCDATA is skipped over by that search as it is all assumed to be page content. In HTML the content of a script tag is treated as CDATA which means that we can use less than and ampersand in our script without it being misinterpreted as HTML. XHTML treats the content of script tags as PCDATA unless we tell it differently and so any script included into your page that includes less thans and/or ampersands will break as they will be treated as XHTML instead of as JavaScript. We get around this by enclosing our script content inside a CDATA tag in order to tell the browser to process the code correctly.

<script type="application/x-javascript">
<![CDATA[
... script code goes here ...
]]>
</script>

By coding our script this way the browser can now easily distinguish what parts of the page are XHTML and what are JavaScript. We don't have to worry about JScript in this instance since none of the browsers that support JScript support XHTML.

Of course rather than worrying about whatr you should suppound your script code inside of, if anything, the far simpler solution is to put the JavaScript/JScript code into its own separate file and add a src attribute to the script tag instead. That way you don't have any content between the open and close script tags to be misinterpreted if you get things wrong.

Related Video
How to Add Comments in Excel
Create Tables in HTML

©2013 About.com. All rights reserved.