HTMLScriptElement

« HTMLScriptElement

HTML script elements expose the HTMLScriptElement interface, which provides special properties and methods (beyond the regular HTMLElement object interface they also have available to them by inheritance) for manipulating the layout and presentation of <script> elements.

Properties

Inherits properties from its parent, HTMLElement.

Name Type Description
type DOMString Represents the MIME type of the script. It reflects the type attribute.
src DOMString Represents gives the address of the external script resource to use. It reflects the src attribute.
htmlFor DOMString The htmlFor property sets or returns the value of the for attribute of a label. The for attribute specifies which form element a label is bound to.
event DOMString HTML DOM events allow JavaScript to register different event handlers on elements in an HTML document.
charset DOMString Represents the character encoding of the external script resource. It reflects the charset attribute.
async Boolean

The async and defer attributes are boolean attributes that indicate how the script should be executed. The defer and async attributes must not be specified if the src attribute is not present.

There are three possible modes that can be selected using these attributes. If the async attribute is present, then the script will be executed asynchronously, as soon as it is available. If the async attribute is not present but the defer attribute is present, then the script is executed when the page has finished parsing. If neither attribute is present, then the script is fetched and executed immediately, before the user agent continues parsing the page.

Note: The exact processing details for these attributes are, for mostly historical reasons, somewhat non-trivial, involving a number of aspects of HTML. The implementation requirements are therefore by necessity scattered throughout the specification. These algorithms describe the core of this processing, but these algorithms reference and are referenced by the parsing rules for <script> start and end tags in HTML, in foreign content, and in XML, the rules for the document.write() method, the handling of scripting, etc.

The defer attribute may be specified even if the async attribute is specified, to cause legacy Web browsers that only support defer (and not async) to fall back to the defer behavior instead of the synchronous blocking behavior that is the default.

defer Boolean
crossOrigin DOMString Is a DOMString that corresponds to the CORS setting for this script element. See CORS settings attributes for details. It controls, for scripts that are obtained from other origins, whether error information will be exposed.
text DOMString

The IDL attribute text must return a concatenation of the contents of all the Text nodes that are children of the <script> element (ignoring any other nodes such as comments or elements), in tree order. On setting, it must act the same way as the textContent IDL attribute.

Note: When inserted using the document.write() method, <script> elements execute (typically synchronously), but when inserted using innerHTML and outerHTML attributes, they do not execute at all.

Methods

No specific method; inherits properties from its parent, HTMLElement.

Examples

Example #1: Dynamically importing scripts

Let's create a function named importScript(url[, onloadFunction]) able to import new scripts within a document creating a <script> node immediately before the <script> which hosts the following code (got through document.currentScript). These scripts will be asynchronously executed. For more details, see the defer and async properties.

function loadError (oError) {
  throw new URIError("The script " + oError.target.src + " is not accessible.");
}

function importScript (sSrc, fOnload) {
  var oScript = document.createElement("script");
  oScript.type = "text\/javascript";
  oScript.onerror = loadError;
  if (fOnload) { oScript.onload = fOnload; }
  document.currentScript.parentNode.insertBefore(oScript, document.currentScript);
  oScript.src = sSrc;
}

…the same thing, but appending the new scripts as last childs of the <head> tag, instead of appending them immediately before the document.currentScript element:

var importScript = (function (oHead) {

  function loadError (oError) {
    throw new URIError("The script " + oError.target.src + " is not accessible.");
  }

  return function (sSrc, fOnload) {
    var oScript = document.createElement("script");
    oScript.type = "text\/javascript";
    oScript.onerror = loadError;
    if (fOnload) { oScript.onload = fOnload; }
    oHead.appendChild(oScript);
    oScript.src = sSrc;
  }

})(document.getElementsByTagName("head")[0]);

Sample usage:

importScript("myScript1.js");
importScript("myScript2.js", /* onload function: */ function () { alert("You read this alert because the script \"myScript2.js\" has been correctly loaded."); });

Specifications

Specification Status Comment
WHATWG HTML Living Standard Living Standard No change from HTML5.
HTML5 Candidate Recommendation The following properties are now obsolete: htmlFor,.
Document Object Model (DOM) Level 2 HTML Specification Recommendation No change from Document Object Model (DOM) Level 1 Specification.
Document Object Model (DOM) Level 1 Specification Recommendation Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1.0 1.0 (1.7 or earlier) (Yes) (Yes) (Yes)
async (Yes) 3.6 (1.9.2) 10 Not supported (Yes)
defer (Yes) 3.5 (1.9.1)

4 (follows a spec of its own)

10 (by the spec)

Not supported (Yes)
crossOrigin WebKit bug 81438 13 (13) bug 696301 Not supported Not supported WebKit bug 81438
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) 1.0 (1.0) (Yes) (Yes) (Yes)
async (Yes) 1.0 (1.0) Not supported ? (Yes)
defer (Yes) 1.0 (1.0) Not supported ? (Yes)
crossOrigin ? ? ? ? ?

Gecko-specific notes

Starting in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1), 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 the async property to false on them.

Also, <script> elements inside <iframe>, <noembed> and <noframes> elements are now executed, for the same reasons.

See also

Document Tags and Contributors

Contributors to this page: Sheppy, teoli, kscarfone, fusionchess, Danacus, lborgman
Last updated by: teoli,