<script>

  • Revision slug: Web/HTML/Element/script
  • Revision title: <script>
  • Revision id: 434165
  • Created:
  • Creator: acdha
  • Is current revision? No
  • Comment Updated to show that Chromium did not pick up WebKit's <script crossorigin> support

Revision Content

Summary

The HTML <script> element is used to embed or reference an executable script within an HTML or XHTML document.

Scripts without async or defer attributes are fetched and executed immediately, before the browser continues to parse the page.

Attributes

This element includes the global attributes.

{{htmlattrdef("async")}} {{HTMLVersionInline(5)}}
Set this Boolean attribute to indicate that the browser should, if possible, execute the script asynchronously. It has no effect on inline scripts (i.e., scripts that don't have the src attribute). In older browsers that don't support the async attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox. In Firefox 4.0, the async DOM property defaults to true for script-created scripts, so the default behavior matches the behavior of IE and WebKit. To request script-inserted external scripts be executed in the insertion order in browsers where the document.createElement("script").async evaluates to true (such as Firefox 4.0), set .async=false on the scripts you want to maintain order. Never call document.write() from an async script. In Gecko 1.9.2, calling document.write() has an unpredictable effect. In Gecko 2.0, calling document.write() from an async script has no effect (other than printing a warning to the error console).
{{htmlattrdef("src")}}
This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.
{{htmlattrdef("type")}}
This attribute identifies the scripting language of code embedded within a script element or referenced via the element’s src attribute. This is specified as a MIME type; examples of supported MIME types include text/javascript, text/ecmascript, application/javascript, and application/ecmascript. If this attribute is absent, the script is treated as JavaScript.
{{htmlattrdef("language")}} {{Deprecated_inline}}
Like the type attribute, this attribute identifies the scripting language in use. Unlike the type attribute, however, this attribute’s possible values were never standardized. The type attribute should be used instead.
{{htmlattrdef("defer")}}
This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed. Since this feature hasn't yet been implemented by all other major browsers, authors should not assume that the script’s execution will actually be deferred. Never call document.write() from a defer script (since Gecko 1.9.2, this will blow away the document). The defer attribute shouldn't be used on scripts that don't have the src attribute. Since Gecko 1.9.2, the defer attribute is ignored on scripts that don't have the src attribute. However, in Gecko 1.9.1 even inline scripts are deferred if the defer attribute is set.
{{htmlattrdef("crossorigin")}} {{Non-standard_inline}}
Normal script tags will pass minimal information to the {{domxref('window.onerror')}} for scripts which do not pass the standard CORS checks. To allow error logging for sites which use a separate domain for static media, several browsers have enabled the crossorigin attribute for scripts using the same definition as the standard img {{htmlattrxref('crossorigin', 'img')}} attribute. Efforts to standardize this attribute are underway on the WHATWG mailing list.

Examples

<!-- HTML4 and (x)HTML -->
<script type="text/javascript" src="javascript.js">

<!-- HTML5 -->
<script src="javascript.js"></script>

Specifications

Specification Status Comments
{{SpecName('HTML5 W3C', 'scripting-1.html#script', '<script>')}} {{Spec2('HTML5 W3C')}}  
{{SpecName('HTML4.01', 'interact/scripts.html#h-18.2.1', '<script>')}} {{Spec2('HTML4.01')}}  

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 {{CompatGeckoDesktop("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
async attribute {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.9.2")}} 10 {{CompatNo}} {{CompatVersionUnknown}}
defer attribute {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.9.1")}}

4 (follows a spec of its own)

10 (by the spec)

{{CompatNo}} {{CompatVersionUnknown}}
crossorigin attribute {{CompatNo}} Chromium Bug 159566 {{CompatGeckoDesktop("13")}} {{bug(696301)}} {{CompatNo}} 12.50 {{CompatVersionUnknown}}

 ({{WebKitBug(81438)}})

Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
async attribute {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatNo}} {{CompatUnknown}} {{CompatVersionUnknown}}
defer attribute {{CompatVersionUnknown}} {{CompatGeckoMobile("1.0")}} {{CompatNo}} {{CompatUnknown}} {{CompatVersionUnknown}}

Gecko-specific notes

Starting in Gecko 2.0 {{geckoRelease("2.0")}}, inserting script elements that have been created by calling document.createElement("script") into the DOM no longer enforces execution in insertion order. This change lets Gecko properly abide by the HTML5 specification. To make script-inserted external scripts execute in their insertion order, set .async=false on them.

Also, {{HTMLElement("script")}} elements inside {{HTMLElement("iframe")}}, {{HTMLElement("noembed")}} and {{HTMLElement("noframes")}} elements are now executed, for the same reasons.

See also

{{HTML:Element_Navigation}}

Revision Source

<h2 id="Summary">Summary</h2>
<p>The <strong>HTML <code>&lt;script&gt;</code> element</strong> is used to embed or reference an executable script within an <abbr title="Hypertext Markup Language">HTML</abbr> or <abbr title="Extensible Hypertext Markup Language">XHTML</abbr> document.</p>
<p>Scripts without <code>async</code> or <code>defer</code> attributes are fetched and executed immediately, before the browser continues to parse the page.</p>
<ul class="htmlelt">
  <li><dfn><a href="/en-US/docs/Web/HTML/Content_categories" title="HTML/Content_categories">Content categories</a></dfn> <a href="/en-US/docs/Web/HTML/Content_categories#Metadata_content" title="HTML/Content_categories#Metadata_content">Metadata content</a>, <a href="/en-US/docs/Web/HTML/Content_categories#Flow_content" title="HTML/Content_categories#Flow_content">Flow content</a>, <a href="/en-US/docs/Web/HTML/Content_categories#Phrasing_content" title="HTML/Content_categories#Phrasing_content">Phrasing content</a>.</li>
  <li><dfn>Permitted content</dfn> Dynamic script such as <code>text/javascript</code>.</li>
  <li><dfn>Tag omission</dfn> {{no_tag_omission}}</li>
  <li><dfn>Permitted parent elements</dfn> Any element that accepts <a href="/en-US/docs/Web/HTML/Content_categories#Metadata_content" title="HTML/Content_categories#Metadata_content">metadata content</a>, or any element that accepts phrasing content.</li>
  <li><dfn>DOM interface</dfn>{{domxref("HTMLScriptElement")}}</li>
</ul>
<h2 id="Attributes">Attributes</h2>
<p>This element includes the <a href="/en-US/docs/Web/HTML/Global_attributes">global attributes</a>.</p>
<dl>
  <dt>
    {{htmlattrdef("async")}} {{HTMLVersionInline(5)}}</dt>
  <dd>
    Set this Boolean attribute to indicate that the browser should, if possible, execute the script asynchronously. It has no effect on inline scripts (i.e., scripts that don't have the <strong>src</strong> attribute). In older browsers that don't support the <strong>async</strong> attribute, parser-inserted scripts block the parser; script-inserted scripts execute asynchronously in IE and WebKit, but synchronously in Opera and pre-4.0 Firefox. In Firefox 4.0, the <code>async</code> DOM&nbsp;property defaults to <code>true</code> for script-created scripts, so the default behavior matches the behavior of IE&nbsp;and WebKit. To request script-inserted external scripts be executed in the insertion order in browsers where the <code>document.createElement("script").async</code> evaluates to <code>true</code> (such as Firefox 4.0), set <code>.async=false</code> on the scripts you want to maintain order. Never call <code>document.write()</code> from an <code>async</code> script. In Gecko 1.9.2, calling <code>document.write()</code> has an unpredictable effect. In Gecko 2.0, calling <code>document.write()</code> from an <code>async</code> script has no effect (other than printing a warning to the error console).</dd>
  <dt>
    {{htmlattrdef("src")}}</dt>
  <dd>
    This attribute specifies the <abbr>URI</abbr> of an external script; this can be used as an alternative to embedding a script directly within a document. <code>script</code> elements with an <code>src</code> attribute specified should not have a script embedded within its tags.</dd>
  <dt>
    {{htmlattrdef("type")}}</dt>
  <dd>
    This attribute identifies the scripting language of code embedded within a <code>script</code> element or referenced via the element’s <code>src</code> attribute. This is specified as a <abbr title="Multi-purpose Internet Mail Extensions">MIME</abbr> type; examples of supported <abbr title="Multi-purpose Internet Mail Extensions">MIME</abbr> types include <code>text/javascript</code>, <code>text/ecmascript</code>, <code>application/javascript</code>, and <code>application/ecmascript</code>. If this attribute is absent, the script is treated as JavaScript.</dd>
  <dt>
    {{htmlattrdef("language")}} {{Deprecated_inline}}</dt>
  <dd>
    Like the <code>type</code> attribute, this attribute identifies the scripting language in use. Unlike the <code>type</code> attribute, however, this attribute’s possible values were never standardized. The <code>type</code> attribute should be used instead.</dd>
  <dt>
    {{htmlattrdef("defer")}}</dt>
  <dd>
    This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed. Since this feature hasn't yet been implemented by all other major browsers, authors should not assume that the script’s execution will actually be deferred. Never call <code>document.write()</code> from a <code>defer</code> script (since Gecko 1.9.2, this will blow away the document). The <code>defer</code> attribute shouldn't be used on scripts that don't have the <code>src</code> attribute. Since Gecko 1.9.2, the <code>defer</code> attribute is ignored on scripts that don't have the <code>src</code> attribute. However, in Gecko 1.9.1 even inline scripts are deferred if the <code>defer</code> attribute is set.</dd>
  <dt>
    {{htmlattrdef("crossorigin")}} {{Non-standard_inline}}</dt>
  <dd>
    Normal <code>script</code> tags will pass minimal information to the {{domxref('window.onerror')}} for scripts which do not pass the standard <a href="/en-US/docs/HTTP_access_control">CORS</a> checks. To allow error logging for sites which use a separate domain for static media, several browsers have enabled the <code>crossorigin</code> attribute for scripts using the same definition as the standard img {{htmlattrxref('crossorigin', 'img')}} attribute. Efforts to standardize this attribute are underway on the <a href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2012-February/034969.html">WHATWG mailing list</a>.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<pre class="brush: html">
&lt;!-- HTML4 and (x)HTML --&gt;
&lt;script type="text/javascript" src="javascript.js"&gt;

&lt;!-- HTML5 --&gt;
&lt;script src="javascript.js"&gt;&lt;/script&gt;
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table" height="75" width="526">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comments</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('HTML5 W3C', 'scripting-1.html#script', '&lt;script&gt;')}}</td>
      <td>{{Spec2('HTML5 W3C')}}</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>{{SpecName('HTML4.01', 'interact/scripts.html#h-18.2.1', '&lt;script&gt;')}}</td>
      <td>{{Spec2('HTML4.01')}}</td>
      <td>&nbsp;</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>
  {{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1.0</td>
        <td>{{CompatGeckoDesktop("1.0")}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
      <tr>
        <td>async attribute</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoDesktop("1.9.2")}}</td>
        <td>10</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
      <tr>
        <td>defer attribute</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoDesktop("1.9.1")}}</td>
        <td>
          <p>4 (follows a spec of its own)</p>
          <p>10 (by the spec)</p>
        </td>
        <td>{{CompatNo}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
      <tr>
        <td>crossorigin attribute</td>
        <td>{{CompatNo}}&nbsp;<a href="http://crbug.com/159566" title="http://crbug.com/159566">Chromium Bug 159566</a></td>
        <td>{{CompatGeckoDesktop("13")}} {{bug(696301)}}</td>
        <td>{{CompatNo}}</td>
        <td>12.50</td>
        <td>{{CompatVersionUnknown}}<br />
          <p>&nbsp;<span style="line-height: 1.572;">({{WebKitBug(81438)}})</span></p>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoMobile("1.0")}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
      <tr>
        <td>async attribute</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoMobile("1.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
      <tr>
        <td>defer attribute</td>
        <td>{{CompatVersionUnknown}}</td>
        <td>{{CompatGeckoMobile("1.0")}}</td>
        <td>{{CompatNo}}</td>
        <td>{{CompatUnknown}}</td>
        <td>{{CompatVersionUnknown}}</td>
      </tr>
    </tbody>
  </table>
</div>
<h3 id="Gecko-specific_notes">Gecko-specific notes</h3>
<p>Starting in Gecko 2.0 {{geckoRelease("2.0")}}, inserting script elements that have been created by calling document.createElement("script") into the DOM no longer enforces execution in insertion order. This change lets Gecko properly abide by the HTML5 specification. To make script-inserted external scripts execute in their insertion order, set .async=false on them.</p>
<p>Also, {{HTMLElement("script")}} elements inside {{HTMLElement("iframe")}}, {{HTMLElement("noembed")}} and {{HTMLElement("noframes")}} elements are now executed, for the same reasons.</p>
<h2 id="See_also">See also</h2>
<ul>
  <li>{{domxref("document.currentScript")}}</li>
  <li><a href="http://pieisgood.org/test/script-link-events/">Ryan Grove's &lt;script&gt; and &lt;link&gt; node event compatibility chart</a></li>
</ul>
<div>
  {{HTML:Element_Navigation}}</div>
Revert to this revision