HTML Tags
Advertisements

HTML <script> Tag

Topic: HTML5 Tags ReferencePrev|Next

Description

The <script> tag is used to embed or reference an executable client-side script (such as JavaScript) within an HTML or XHTML document. The <script> tag may appear any number of times in the <head> or <body> of an HTML document.

The <script> element either contains a series of scripting statements, or it points to an external script file (through the src attribute) that's processed on the client side (user's computer) to add interactivity or affect the behavior of web pages.

The following table summarizes the usages context and the version history of this tag.

Placement: Inline (but can also be contained in <head>)
Content: Text
Start/End Tag: Start tag: required, End tag: required
Version: HTML 4, 4.01, 5

Tip: Client-side scripting refers to the type of computer programs that are executed client-side, by the user's web browser. JavaScript is the most popular client-side scripting language on the web.


Syntax

The basic syntax of the <script> tag is given with:

HTML / XHTML: <script type="text/javascript"> ... </script>

The example below shows the <script> tag in action.

<script>
    document.write("Hello World!");
</script>

Syntax (XHTML)

There are some important differences in the way that HTML and XHTML deal with the content inside the scripts. In HTML, the content type is declared as CDATA, which means that HTML entities will not be parsed. However, in XHTML, the content type is declared as #PCDATA, which means that entities will be parsed.

To ensure that the content inside the opening <script> and closing </script> tags parses correctly when it is included within an XHTML document, all special characters should be encoded — for example, ampersands (&) should be encoded as &amp;, and greater-than (>) symbols should be encoded as &gt; and so on, or all content should be wrapped inside a CDATA section like this:

<script>
    // <![CDATA[
    . . . JavaScript code goes here . . .
    // ]]>
</script>

Note: If the src attribute is specified, the <script> element should not have a script embedded within its tags.


Tag-Specific Attributes

The following table shows the attributes that are specific to the <script> tag.

Attribute Value Description
async async This Boolean attribute specifies that the script should be executed asynchronously, as soon as it becomes available. Only for external scripts, it has no effect on inline scripts.
type content-type Specifies the language of the script. The most common value is text/javascript, which indicates a JavaScript language.
charset charset Specifies the character encoding of the external script file. This attribute must not be specified if src attribute is not present.
defer defer This boolean attribute Specifies that script should be executed after the document has been parsed. This attribute shouldn't be used on scripts that don't have the src attribute.
src URL Specifies the location of an external script file.
xml:space preserve Obsolete Specifies whether whitespace should be preserved within the script element.

Global Attributes

Like all other HTML tags, the <script> tag supports the global attributes in HTML5.


Event Attributes

The <script> tag also supports the event attributes in HTML5.


Browser Compatibility

The <script> tag is supported in all major modern browsers.

Browsers Icon

Basic Support—

  • Firefox 1+
  • Google Chrome 1+
  • Internet Explorer 3+
  • Apple Safari 1+
  • Opera 4+

Tip: Check out the <noscript> element which provides an alternate content for users that have either disabled scripts in their browser or have a browser that doesn't support client-side scripting.


Further Reading

See tutorial on: HTML Script.

Related tag: <noscript>.

Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties