script
Definition
The script
element places a client-side script, such as JavaScript, within a document. This element may appear any number of times in the head
or body
of a Web page. The script
element may contain a script (called an embedded script) or point via the src
attribute to a file containing a script (an external script).
Example
The following is an example of embedded script:
<script type="text/javascript">
function resetValue {
var objects = document.getElementsByTagName('object');
for (var i=0; i<objects.length; i++) {
var obj = objects[i];
obj.value = '';
}
}
</script>
This is an example of external script:
<script type="text/javascript" src="menu.js"></script>
Best practice
If the script
element contains embedded script and that script contains XHTML markup characters such as <
, >
, &
and "
, then the script should be enclosed in CDATA
. For backward compatibility (when XHTML is served as HTML), put //
comments before the opening and the closing CDATA
markup. For example:
<script type="text/javascript">
//<![CDATA[
alert('Tom & I want to say "10 > 9"');
//]]>
</script>
Attributes
Basic
src
- (URI) Location of an external script.
type
- (ContentType) This attribute specifies the scripting language of the element's contents. The scripting language is specified as a content type. For example:
text/javascript
. This attribute is required.
Advanced
charset
- (Charset) Character encoding of the resource designated by
src
. defer
- When set, this attribute provides a hint to the Web browser that the script is not going to generate any document content (no
document.write
in javascript for example), permitting the Web browser to continue parsing and rendering the rest of the page. Possible value is defer
. xml:space
- This attribute indicates if white space (extra spaces, tabs) should be preserved. Possible value is
preserve
.
Common core attributes
class
- (NameTokens) This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or set of class names. Multiple class names must be separated by white space characters. Class names are typically used to apply CSS formatting rules to an element.
id
- (ID) This attribute assigns an ID to an element. This ID must be unique in a document. This ID can be used by client-side scripts (such as JavaScript) to select elements, apply CSS formatting rules, or to build relationships between elements.
title
- (Text) This attribute offers advisory information. Some Web browsers will display this information as tooltips. Assistive technologies may make this information available to users as additional information about the element.
Common internationalization attributes
xml:lang
- (NameToken) This attribute specifies the base language of an element's attribute values and text content.
dir
This attribute specifies the base direction of text. Possible values:
ltr
: Left-to-rightrtl
: Right-to-left
Common event attributes
onclick
- (Script) A client-side script event that occurs when a pointing device button is clicked over an element.
ondblclick
- (Script) A client-side script event that occurs when a pointing device button is double-clicked over an element.
onmousedown
- (Script) A client-side script event that occurs when a pointing device button is pressed down over an element.
onmouseup
- (Script) A client-side script event that occurs when a pointing device button is released over an element.
onmouseover
- (Script) A client-side script event that occurs when a pointing device is moved onto an element.
onmousemove
- (Script) A client-side script event that occurs when a pointing device is moved within an element.
onmouseout
- (Script) A client-side script event that occurs when a pointing device is moved away from an element.
onkeypress
- (Script) A client-side script event that occurs when a key is pressed down over an element then released.
onkeydown
- (Script) A client-side script event that occurs when a key is pressed down over an element.
onkeyup
- (Script) A client-side script event that occurs when a key is released over an element.
Common style attribute
style
- (Text) This attribute specifies formatting style information for the current element. The content of this attribute is called inline CSS. The
style
attribute is deprecated (considered outdated), because it fuses together content and formatting.
Contains
See also