document.currentScript

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

Revision Content

{{DomRef}}

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.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 29 {{ CompatGeckoDesktop("2.0") }} {{ CompatUnknown() }} {{ CompatUnknown() }}
{{ CompatUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatUnknown() }} yes {{ CompatUnknown() }} {{ CompatUnknown() }} {{ CompatUnknown() }}

Specification

See also

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

Revision Source

<div>
  {{DomRef}}</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=".E6.B5.8F.E8.A7.88.E5.99.A8.E5.85.BC.E5.AE.B9.E6.80.A7">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<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>29</td>
        <td>{{ CompatGeckoDesktop("2.0") }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>
          <table class="compat-table">
            <tbody>
              <tr>
              </tr>
              <tr>
                <td>{{ CompatUnknown() }}</td>
              </tr>
            </tbody>
          </table>
        </td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for 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>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>yes</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatUnknown() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-currentscript" title="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#dom-document-currentscript">HTML5</a></li>
</ul>
<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