Layout ] Multimedia ] Programming ] Scripting ] Communicate ] Navigation ]

Script Tag Syntax

Up ] [ Script Tag Syntax ] Forwarder Script ]

This is an explanation of how to use the SCRIPT tag, and its attributes.

<SCRIPT>
<!--
script data
//-->
</SCRIPT>

The end tag is required.  All attributes except the Type are optional, though it is best to include both the language and type tag, so that the browser will know which language you are using.  The HTML comment tags are not part of the specification, though they insure backwards compatibility.  The comment end tag is preceded by a //, the JavaScript comment character.  In the script data, you should be careful not to have any </ sequences, as this is formally supposed to end the script.  You should escape them using the JavaScript escape character, the backslash, as follows:

Document.Write "<P>This is a <B>paragraph.</B></P>" would become
Document.Write "<P>This is a <B>paragraph.<\/B><\/P>"

This doesn't look nearly as nice, and is more work, but is necessary to comply with the HTML specification.

Attributes:

  • language: For JavaScript, should use the value "JavaScript".  Supported by IE, Netscape, and Opera 3+. Some people use different values for this to prevent certain browsers from reading the script, such as using "JScript" to prevent Netscape from reading it, or using "JavaScript1.2" to prevent MSIE 3 from reading it, but this is not standardized, and is sometimes unreliable. The HTML 4 specification prefers the Type attribute.
  • type: The MIME type of the scripting language.  Suggested over the language attribute as the manner of specifying the scripting language in the HTML 4 specification, but not supported by any browsers currently.  For JavaScript, the value should be "text/javascript".
  • src: The URL of an external source file.  Supported by IE4+ and NN3+.
  • id: Common core attribute.  A global identifier for this script.  Supported by IE4+.
  • title: Common core attribute.  Provides extra information about the script.   Supported by IE4+.
  • for: Name or ID of a page element, for which this is an event handler.  Used with the Event attribute.  Name always takes precedence.  Supported by IE4+.
  • event: The event that this script handles.  Used with the For attribute.   Supported by IE4+.

The default scripting language can be set using a META tag in the HEAD, though this is not supported by current browsers.

<META http-equiv="Content-Script-Type" content="text/javascript">

If you wish to have HTML content shown if the user's browser does not support scripting, you should use the NOSCRIPT tag.

<NOSCRIPT>
alternate content
</NOSCRIPT>

NOSCRIPT is shown by script-aware browsers if scripting is disabled or a scripting language which it did not understand was used.  NOSCRIPT supports all core attributes, international attributes, and events, though they are not needed.


If you wish to make pages which are widely compatible and will work with the next generation of browsers without breaking, it is wise to use the Type AND Language attributes, while avoiding the Src, For and Event attributes.  You should also provide alternate content.  For example:

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
<!--
script data
//-->
</SCRIPT>
<NOSCRIPT>
alternate data
</NOSCRIPT>

This will work with browsers which support the Type attribute, which will then ignore the Language attribute.  Also, it will work with browsers which support the more common Language attribute, which will ignore the Type attribute.  If they don't support either, they most probably don't support JavaScript.  If you are willing to exclude users with IE3, you can use the Src attribute and store your scripts in external files.  This can save you work, as you don't have to put it in each document, and allows you to keep the user from seeing the script if they open the HTML source.   However, it excludes users with IE3, which could usually use JavaScript.

The Title tag is available if you wish to provide more information about the script.   Some information that you might use it for is the author's name and email address, and the purpose of the script.

Most scripts, especially those containing shared functions, should be put in the HEAD of the document, so that they are loaded before the rest of the page.

Layout ] Multimedia ] Programming ] Scripting ] Communicate ] Navigation ]
[ Script Tag Syntax ] Forwarder Script ]

The Web Wizard's Spellbook © 1998, Team WWS