Little Web Hut

HTML 4.01 and XHTML 1.0 Reference: <script> Tag

HTML <script> Tag
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.

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>
Test It HTML StyleTest It XHTML Style

Content Model The <script> element can contain Text between its opening and closing tags.

Red attributes have been deprecated
<script> Tag Attribute Summary
Required Tag Specific Core Focus Events Language
type charset defer language src xml:space1 id1      
Notes: 1. XHTML only.

Red attributes have been deprecated
<script> Tag Attributes
Attribute Description
typeThe 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>
Test It HTML StyleTest It XHTML Style


Note: This is a required attribute.
charsetThe 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>
deferThe 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.

HTML & XHTML <script> example using an external JavaScript file and the type, src and defer attributes.
<p>Color Choices: Red, Green, Blue.</p>
<script type="text/javascript" src="sample.js" defer="defer">
</script>
<p>Size Choices: 1, 3, 5.</p>
Test It HTML StyleTest It XHTML Style

languageThe 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.
srcThe 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.

HTML & XHTML <script> example using an external JavaScript file and the type and src attributes.
<p>Color Choices: Red, Green, Blue.</p>
<script type="text/javascript" src="sample.js">
</script>
<p>Size Choices: 1, 3, 5.</p>
Test It HTML StyleTest It XHTML Style

xml:spaceThe 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.
idThe 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.

HTML <script> Tag Examples
Test It HTML StyleTest It HTML Style<script> example with JavaScript contained between the opening and closing <script> tags.
Test It HTML StyleTest It HTML Style<script> example using an external JavaScript file and the type, src and defer attributes.
Test It HTML StyleTest It HTML Style<script> example using an external JavaScript file and the type and src attributes.