document.currentScript

  • Revision slug: DOM/document.currentScript
  • Revision title: document.currentScript
  • Revision id: 333961
  • Created:
  • Creator: Midnightaz
  • 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

HTML5

See also

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

{{ languages( {"zh-cn": "zh-cn/DOM/document.currentScript" } ) }}

Revision Source

<p>{{ DomRef() }}{{ gecko_minversion_header("2.0") }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Returns the {{ HTMLElement("script") }} element whose script is currently being processed.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval">
var curScriptElement = document.currentScript;
</pre>
<h3 id="Example" name="Example">Example</h3>
<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>
<h3 id="Notes">Notes</h3>
<p>It's important to note that this will not reference the {{ HTMLElement("script") }}&nbsp;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>
<h3 id="Specification" name="Specification">Specification</h3>
<p><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></p>
<h3 id="See_also">See also</h3>
<ul>
  <li>{{ HTMLElement("script") }}</li>
  <li>{{ domxref("element.onafterscriptexecute") }}</li>
  <li>{{ domxref("element.onbeforescriptexecute") }}</li>
</ul>
<p>{{ languages( {"zh-cn": "zh-cn/DOM/document.currentScript" } ) }}</p>
Revert to this revision