HTML Script Element

The HTML script element is used to embed script in the header of an HTML document. The HTML script element may be included inside either the HTML head element or HTML body element. HTML script element attributes include:

  • CHARSET - The character encoding of the script such as "ISO-8859-1".
  • DEFER - The script will not be parsed until the document is loaded.
  • LANGUAGE - Describes the name of the script language. This attribute is depreciated.
  • SRC - The external location where the script may be. In this case the script in between the SCRIPT tags is not used, but an external file is loaded.
  • TYPE - Describes the content type of the script language.

The script embedded in the header between the <script> and </script> tags can be used to take actions based on user actions or it may be perform functions which are invoked by a timer. One item the client side script program may perform is to check user input to a form and action may be taken when the user submits the form or clicks on a button.

Javascript code is normally hidden within HTML comments to prevent older browsers from displaying it as part of the HTML contents. An example of the use of embedding javascript is as follows:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>An Example of JavaScript code</title>
<script type="text/javascript" language="JavaScript">
<!-- Use the HTML comment to hide JavaScript from old browsers
document.write("This text was written using JavaScript.")
// End hiding JavaScript -->
You see this if your browser won't run JavaScript.

The language attribute in the example may be left out.