JavaScript Wrappers

Over the years there have been a number of different ways to write the tags that you wrap around your JavaScript (or for IE3 through 8 - jScript). Here we will look at each of the alternative wrappers and what it was or is used for.


<script language="javascript">
<!--
 
// -->
</script>

This was the original wrapper to use around your JavaScript in NetScape 2 and 3 and jScript in Internet Explorer 3. The HTML comment tag inside of the script tag was there because earlier browsers such as Internet Explorer 2, Netscape 1, Mozaic and WWW did not understand the script tag and so would try to render the content of the script tag as if it were content for your page instead of a script to be run. Once everyone had upgraded their browser to Netscape 4 or Internet Explorer 4 this particular wrapper had become obsolete.


<script type="text/jscript">
/*@cc_on
  @if (@_jscript)
 
  @end
@*/
</script>

This is the wrapper that you should use when your script is specifically written in jScript and you don't want it to run in browsers that only understand JavaScript. The script enclosed inside this wrapper will only run in Internet Explorer. The jScript conditional comment included in the wrapper is needed because Google Chrome, Safari, and Opera ignore the MIME type and attempt to run the script as JavaScript even though it is clearly identified as jScript. Firefox does obey the MIME type and so would not run the script even if the conditional comment were omitted.


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

This wrapper identifies that the script is written to run as either JavaScript or jScript in a web page written in HTML. This particular MIME type is flagged as obsolete but is the only one that allows you to provide one script that can be run in both languages. Most servers are configured to use this MIME type for scripts where the type is not specified in the tag and so scripts where the type is omitted (such as those using the language attribute instead or with no attributes in the script tag at all) will usually run as if the tag were written this way. It is of course possible to configure the server ti use a different MIME type instead.


<script type="application/javascript">
 
</script>

This is the wrapper to use when thew web page is written in HTML and the script is written in JavaScript. Versions of Internet Explorer that do not understand JavaScript (that is IE8 and earlier) will not run the script.


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

Some content management systems strip the HTML comments out of the page content before sending it to the browser. This allows the page author to document their code as much as they like without slowing the download by actually sending all the comments to the browser. When a CMS that does this encounters a script enclosed in an HTML comment like this it strips it from the page before sending the HTML to the browser. This is therefore a useful way to comment out scripts when using a CMS. When used this way the script tag would be completely empty if you view the page source in a browser. If you come across web pages where the script is visible in the source it means that the person who wrote the script has only partly updated the wrapper and doesn't properly understand the content of this page.


<script type="application/javascript">
<![CDATA[
 
]]>
</script>

This is the wrapper to use when thew web page is written in XHTML and we are using embedded JavaScript. Since browsers that only understand jScript don't understand XHTML we do not need to cater for jScript in pages written in XHTML. Unlike HTML, XHTML would treat the content of the script tag as PCDATA unless we include the tag identifying it as CDATA.


<script type="text/javascript">
// <![CDATA[
 
// ]]>
</script>

This wrapper indicates that the page is written in HTML but that the page is being validated as if it were XHTML. The commented CDATA tag is there simply to tell the validator to treat the content of the script as CDATA. HTML treats it as CDATA anyway and so does not need the tag there to identify it as such.


<script language="php">
 
</script>

If you see script tags that look like this then it means that you are looking at code on the server and that the script tag is identifying the server side language that the script contained inside the tag is written in (in this example PHP). These scripts will be processed entirely on the server and the script tag will not be included in the HTML delivered to the web page.


 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate