Syntax | This element uses separate opening and closing tags.<script>...</script> |
---|---|
Usage | The <script> tag allows a script to be added. JavaScript is a a popular scripting language. The <script> tag can appear in the head or the body section and may occur multiple times. The script is defined between the opening and closing <script> tags or alternately in an external file that is specified using the src attribute. If the browser does not support scripts, or if scripting is not enabled, then the <noscript> tag can be used to display alternate content. |
Content Model | The <script> element can contain Text between its opening and closing tags. |
Required | Tag Specific | Core | Focus | Events | Language |
---|---|---|---|---|---|
type | charset defer language src xml:space1 | id1 | |||
Notes: 1. XHTML only. |
Attribute | Description |
---|---|
type | The type attribute specifies the scripting language. For JavaScript, the value "text/javascript" can be used. HTML & XHTML <script> example with JavaScript contained between the opening and closing <script> tags. <script type="text/javascript">
document.write("JavaScript Sample")
</script>
<noscript>
<p>This browser does not support scripts or scripting is disabled.</p>
</noscript> Note: This is a required attribute. |
charset | The charset attribute specifies the character encoding for the script that is specified using the src attribute. The charset attribute does not apply to the contents between the opening and closing <script> tags. Therefore, if the src attribute is not used then the charset attribute does not apply. <script> tag example using the charset attribute. <script type="text/javascript" src="sample.js" charset="utf-8">
</script> |
defer | The defer attribute can be used to tell the browser that the script does not generate any web page content. The browser can optionally continue rendering the rest of the web page and defer execution of the script. |
language | The language attribute specifies the scripting language. Since this attribute has been deprecated, it is suggested that the type attribute be used to specify the scripting language. Note: This attribute has been deprecated. |
src | The src attribute specifies the URL of an external script file. If an external script file is specified, the external script is used instead of the contents between the opening and closing <script> tags. |
xml:space | The xml:space attribute is used to specify that whitespace should be preserved. <script> tag example using the xml:space attribute. <script type="text/javascript" xml:space="preserve"> Note: XHTML only. |
id | The id attribute assigns a unique name to a tag. This allows style sheets or scripts to reference the tag. See id Example Note: XHTML only. |