files/en-us/web/api/htmlscriptelement/index.md
{{APIRef("HTML DOM")}}
HTML {{HTMLElement("script")}} elements expose the HTMLScriptElement interface, which provides special properties and methods for manipulating the behavior and execution of <script> elements (beyond the inherited {{domxref("HTMLElement")}} interface).
JavaScript files should be served with the text/javascript MIME type, but browsers are lenient and block them only if the script is served with an image type (image/*), video type (video/*), audio type (audio/*), or text/csv. If the script is blocked, its element receives an {{domxref("HTMLElement/error_event", "error")}} event; otherwise, it receives a {{domxref("Window/load_event", "load")}} event.
[!NOTE] When inserted using the {{domxref("Document.write()")}} method, {{HTMLElement("script")}} elements execute (typically synchronously), but when inserted using {{domxref("Element.innerHTML")}} or {{domxref("Element.outerHTML")}}, they do not execute at all.
{{InheritanceDiagram}}
Inherits properties from its parent, {{domxref("HTMLElement")}}.
attributionsrc attribute on a {{htmlelement("script")}} element programmatically, reflecting the value of that attribute. attributionsrc specifies that you want the browser to send an {{httpheader("Attribution-Reporting-Eligible")}} header along with the script resource request. On the server-side this is used to trigger sending an {{httpheader("Attribution-Reporting-Register-Source")}} or {{httpheader("Attribution-Reporting-Register-Trigger")}} header in the response, to register a JavaScript-based attribution source or attribution trigger, respectively.async property is set to true, the external script will be fetched in parallel to parsing and evaluated as soon as it is available. For module scripts, if the async property is set to true, the script and all their dependencies will be fetched in parallel to parsing and evaluated as soon as they are available.blocking attribute of the {{HTMLElement("script")}} element.HTMLScriptElement.charset {{deprecated_inline}}
charset attribute.defer property is set to true, the external script will be executed after the document has been parsed, but before firing {{domxref("Document/DOMContentLoaded_event", "DOMContentLoaded")}} event. For module scripts, the defer property has no effect.HTMLScriptElement.event {{deprecated_inline}}
high to fetch at a high priority, low to fetch at a low priority, or auto to indicate no preference (which is the default). It reflects the fetchpriority attribute of the {{HTMLElement("script")}} element.integrity attribute of the {{HTMLElement("script")}} element.referrerPolicy HTML attribute indicating which referrer to use when fetching the script, and fetches done by that script.src attribute of the {{HTMLElement("script")}} element.textContent property.text property.type attribute of the {{HTMLElement("script")}} element.true if the browser supports scripts of the specified type and false otherwise.
This method provides a simple and unified method for script-related feature detection.No specific methods; inherits methods from its parent, {{domxref("HTMLElement")}}.
No specific events; inherits events from its parent, {{domxref("HTMLElement")}}.
Let's create a function that imports new scripts within a document creating a {{HTMLElement("script")}} node immediately before the {{HTMLElement("script")}} that hosts the following code (through {{domxref("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} didn't load correctly.`);
}
function prefixScript(url, onloadFunction) {
const newScript = document.createElement("script");
newScript.onerror = loadError;
if (onloadFunction) {
newScript.onload = onloadFunction;
}
document.currentScript.parentNode.insertBefore(
newScript,
document.currentScript,
);
newScript.src = url;
}
This next function, instead of prepending the new scripts immediately before the {{domxref("document.currentScript")}} element, appends them as children of the {{HTMLElement("head")}} tag.
function loadError(oError) {
throw new URIError(`The script ${oError.target.src} didn't load correctly.`);
}
function affixScriptToHead(url, onloadFunction) {
const newScript = document.createElement("script");
newScript.onerror = loadError;
if (onloadFunction) {
newScript.onload = onloadFunction;
}
document.head.appendChild(newScript);
newScript.src = url;
}
Sample usage:
affixScriptToHead("myScript1.js");
affixScriptToHead("myScript2.js", () => {
alert('The script "myScript2.js" has been correctly loaded.');
});
{{domxref("HTMLScriptElement.supports_static", "HTMLScriptElement.supports()")}} provides a unified mechanism for checking whether a browser supports particular types of scripts.
The example below shows how to check for module support, using the existence of the noModule attribute as a fallback.
function checkModuleSupport() {
if ("supports" in HTMLScriptElement) {
return HTMLScriptElement.supports("module");
}
return "noModule" in document.createElement("script");
}
Classic scripts are assumed to be supported on all browsers.
{{Specifications}}
{{Compat}}