HTML » Tags » script

Version: 3.2, 4.0

Compability: Explorer 4, 5  Netscape 4, 6

Syntax:
<script> ... </script>

The <script> tag is used to place script code inside the head and body elements of an HTML code document. For all practical purposes, this tag is primarily used to allow the execution of JavaScript code inside HTML code (and, to a lesser extent, VBScript).

Unfortunately, not all browsers (especially older browsers) recognize this tag. It is recommended that you place an HTML opening comment tag right after the opening script tag and a closing comment tag immediately before the closing /script tag. A JavaScript enabled browser will ignore these comment tags and execute the code. Conversely, a browser that does not recognize JavaScript will treat the contents as simply a comment and not crash your page.
 
The closing tag is mandatory.
 

Examples

Code:
<head>
<title>DevGuru JavaScript Example</title>
<script type="text/javascript">
<!--
function checksubmit()
{
   if (document.formname.firstname.value == "")
   {
      alert("Please enter your first name");
      document.formname.firstname.focus();
      return false;
   }
   if (document.formname.lastname.value == "")
   {
      alert("Please enter your last name");
      document.formname.lastname.focus();
      return false;
   }
   return true;
}
-->
</script>
</head>
Explanation:

This example uses the script tag to place a JavaScript function in the head element to see if two form fields (firstname and lastname) have been left blank.

Language(s): HTML

See Also: