JavaScript Document currentScript Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.currentScript returns the <script> element whose classic (non-module) script is currently being processed, or null (MDN). For modules, use import.meta instead.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.currentScript. Start with this line.

JavaScript
document.currentScript
CallWhat it does
document.currentScriptPrimary signature.

Built into the browser. Read or assign document.currentScript — no import required.

Library declarations
JavaScript
document.currentScript

Parameters

ParameterDescription
Classic <script>Required. Modules are excluded (MDN)
Writable?Optional. Read-only property

Return Value

Return ValueDescription
*HTMLScriptElement or null

JavaScript Examples

Example 1 — Log the Running Script

At top level, currentScript is the <script> element executing now.

JavaScript
const el = document.currentScript;
console.log(el.tagName); // "SCRIPT"
console.log(el.id);      // e.g. "demo"
console.log(!el.src);    // true for inline scripts
Try It Yourself

How It Works

  1. Run at top level inside a classic <script>.
  2. Read the element — MDN returns an HTMLScriptElement.
  3. Inspect attributes like id and whether src is empty (inline).
  4. Not a module — module scripts do not set currentScript.

Example 2 — Check async (MDN)

MDN example: see whether the current script is executing asynchronously.

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

How It Works

  1. Get the element via document.currentScript.
  2. Read .async — reflects the script’s async flag.
  3. Inline scripts usually log “synchronously”.
  4. External async scripts may log “asynchronously” when that attribute is set.

Example 3 — Save the Reference for Callbacks

Capture currentScript immediately; inside a click handler the live property is usually null.

JavaScript
const me = document.currentScript;

document.querySelector("button").addEventListener("click", () => {
  console.log(document.currentScript); // null
  console.log(me && me.id);            // still the saved script
});
Try It Yourself

How It Works

  1. Save at top level while the script is first processed (MDN).
  2. Register a handler that runs later.
  3. Live property is null inside the callback.
  4. Saved variable still works — use it for id, dataset, or src.

Browser Support

Document.currentScript is a standard DOM property. MDN marks it Baseline Widely available (since July 2015).

✓ Baseline · Widely available

Document.currentScript

Returns the classic script element being processed, or null. Safe for everyday classic-script use.

98% Widely available
Google Chrome Yes
Yes
Microsoft Edge Yes
Yes
Mozilla Firefox Yes
Yes
Apple Safari Yes
Yes
Opera Yes
Yes
Internet Explorer IE 11+
Yes*
currentScript Excellent

Bottom line: Use freely in modern browsers for classic scripts. See the MDN currentScript reference.

Frequently Asked Questions

MDN: the <script> element whose classic (non-module) script is currently being processed, or null.
No. MDN marks Document.currentScript as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
MDN: it only references the script element while it is initially being processed — not inside callbacks, timeouts, or event handlers.
No. MDN: it does not apply to JavaScript modules. Use import.meta (for example import.meta.url) instead.
Save a reference while the script first runs: const me = document.currentScript; then use me inside later callbacks.
Common properties include src, async, defer, type, id, and dataset (data-* attributes on the <script> tag).

Next: customElementRegistry

Continue with the Document customElementRegistry property.

customElementRegistry →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful