head tag
body tag
TAG: script
<script> ... </script>
Available in versions: 3.2, 4.0
Browser compatibility: Explorer 4, 5 Netscape 4, 6
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.
Core Attributes
None.
Attributes
charset
The
charset
attribute is used to define the character set that was used to write the script.
defer
The
defer
attribute informs the server that the script will not alter the contents of the page on the server-side. This is intended to allow the server to load the page faster. (In JavaScript, this means no document.write statements.)
language
deprecated 4.0
The
language
attribute is the name of the script language, such as
VBScript, JavaScript, JavaScript 1.1,
and
JavaScript 1.2
. This attribute is deprecated effective with version 4.0. You are now to use the
type
attribute. However, most browsers continue to recognize this attribute.
src
The
src
attribute is the URL of a file that contains a script code. This is useful when you need to use repeatedly a long script program. The browser will load and execute the script as a separate file.
type
The
type
attribute is used to define the script language being used. The two most common values are
text/javascript
and
text/vbscript
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.
Code:
<head>
<title>DevGuru JavaScript Example</title>
<script language="JavaScript">
<!--
function checksubmit()
{
if (document.formname.firstname.value == "")
{
alert("Please enter your full name")
document.formname.firstname.focus()
return false
}
if (document.formname.lastname.value == "")
{
alert("Please enter your email address")
document.formname.lastname.focus()
return false
}
return true
)
-->
</script>
</head>
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information