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 type="text/javascript">
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 &
, and greater-than >
symbols should be encoded as >
and so on, or all content should be wrapped inside a CDATA section like this:
The example below shows the <script>
tag in action.
<script type="text/javascript">
// <![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.
Note: The default value for the enctype attribute is application/x-www-script-urlencoded. The value multipart/script-data should be used in combination with the INPUT element (when type="file").
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
Element | |||||
---|---|---|---|---|---|
<script> | Yes | Yes | Yes | Yes | Yes |
- Installation
- HTML <a> Tag
- HTML <abbr> Tag
- HTML <acronym> Tag
- HTML <address> Tag
- HTML <applet> Tag
- HTML <area> Tag
- HTML <article> Tag
- HTML <aside> Tag
- HTML <audio> Tag
- HTML <base> Tag
- HTML <b> Tag
- HTML <basefont> Tag
- HTML <bdi> Tag
- HTML <bdo> Tag
- HTML <big> Tag
- HTML <blockquote> Tag
- HTML <br> Tag
- HTML <body> Tag
- HTML <center> Tag
- HTML <button> Tag
- HTML <cite> Tag
- HTML <canvas> Tag
- HTML <caption> Tag
- HTML <code> Tag
- HTML <col> Tag
- HTML <dd> Tag
- HTML <colgroup> Tag
- HTML <dfn> Tag
- HTML <del> Tag
- HTML <div> Tag
- HTML <dl> Tag
- HTML <details> Tag
- HTML <dt> Tag
- HTML <em> Tag
- HTML <embed> Tag
- HTML <fieldset> Tag
- HTML <figure> Tag
- HTML <figcaption> Tag
- HTML <font> Tag
- HTML <footer> Tag
- HTML <datalist> Tag
- HTML <frameset> Tag
- HTML <frame> Tag
- HTML5 Global Attributes
- HTML <head> Tag
- HTML <header> Tag
- HTML <hgroup> Tag
- HTML <hr> Tag
- HTML <html> Tag
- HTML <iframe> Tag
- HTML <i> Tag
- HTML <form> Tag
- HTML <input> Tag
- HTML <keygen> Tag
- HTML <legend> Tag
- HTML <ins> Tag
- HTML <kbd> Tag
- HTML <li> Tag
- HTML <link> Tag
- HTML <map> Tag
- HTML <img> Tag
- HTML <mark> Tag
- HTML <menu> Tag
- HTML <meta> Tag
- HTML <meter> Tag
- HTML <nav> Tag
- HTML <noframes> Tag
- HTML <noscript> Tag
- HTML <object> Tag
- HTML <ol> Tag
- HTML <optgroup> Tag
- HTML <option> Tag
- HTML <output> Tag
- HTML <p> Tag
- HTML <param> Tag
- HTML <pre> Tag
- HTML <progress> Tag
- HTML <q> Tag
- HTML <rp> Tag
- HTML <rt> Tag
- HTML <ruby> Tag
- HTML <s> Tag
- HTML <samp> Tag
- HTML <script> Tag
- HTML <section> Tag
- HTML <select> Tag
- HTML <small> Tag
- HTML <source> Tag
- HTML <strike> Tag
- HTML <span> Tag
- HTML <style> Tag
- HTML <strong> Tag
- HTML <sub> Tag
- HTML <summary> Tag
- HTML <sup> Tag
- HTML <table> Tag
- HTML <tbody> Tag
- HTML <td> Tag
- HTML <textarea> Tag
- HTML <th> Tag
- HTML <tfoot> Tag
- HTML <thead> Tag
- HTML <source> Tag
- HTML <tr> Tag
- HTML <tt> Tag
- HTML <title> Tag
- HTML <u> Tag
- HTML <ul> Tag
- HTML <var> Tag
- HTML <video> Tag
- HTML <wbr> Tag
- HTML <dir> Tag