document.currentScript

  • Revision slug: Web/API/document.currentScript
  • Revision title: document.currentScript
  • Revision id: 429491
  • Created:
  • Creator: paul.irish
  • Is current revision? No
  • Comment

Revision Content

{{DomRef}}{{gecko_minversion_header("2.0")}}

Summary

Returns the {{HTMLElement("script")}} element whose script is currently being processed.

Syntax

var curScriptElement = document.currentScript;

Example

This example checks to see if the script is being executed asynchronously:

if (document.currentScript.async) {
  console.log("Executing asynchronously");
} else {
  console.log("Executing synchronously");
}

{{HTMLLiveSample("currentScript.html")}}

Notes

It's important to note that this will not reference the {{HTMLElement("script")}} element if the code in the script is being called as a callback or event handler; it will only reference the element while it's initially being processed.

Specification

Browser Support

Chrome 29 added support for currentScript.

See also

  • {{HTMLElement("script")}}
  • {{domxref("element.onafterscriptexecute")}}
  • {{domxref("element.onbeforescriptexecute")}}

Revision Source

<div>
  {{DomRef}}{{gecko_minversion_header("2.0")}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns the {{HTMLElement("script")}} element whose script is currently being processed.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
var curScriptElement = document.currentScript;
</pre>
<h2 id="Example" name="Example">Example</h2>
<p>This example checks to see if the script is being executed asynchronously:</p>
<pre class="brush:js">
if (document.currentScript.async) {
  console.log("Executing asynchronously");
} else {
  console.log("Executing synchronously");
}</pre>
<p>{{HTMLLiveSample("currentScript.html")}}</p>
<h2 id="Notes">Notes</h2>
<p>It's important to note that this will not reference the {{HTMLElement("script")}} element if the code in the script is being called as a callback or event handler; it will only reference the element while it's initially being processed.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-tree-accessors" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-tree-accessors">HTML5</a></li>
</ul>
<h2>Browser Support</h2>
<p>Chrome 29 <a href="https://chromium.googlesource.com/chromium/blink/+/26567ff" title="https://chromium.googlesource.com/chromium/blink/+/26567ff">added support</a> for currentScript.</p>
<h2 id="See_also">See also</h2>
<ul>
  <li>{{HTMLElement("script")}}</li>
  <li>{{domxref("element.onafterscriptexecute")}}</li>
  <li>{{domxref("element.onbeforescriptexecute")}}</li>
</ul>
Revert to this revision