JavaScript Document currentScript Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.currentScript is a read-only instance property that returns the <script> element whose classic script is currently being processed—or null. Learn when it is set, why callbacks clear it, how modules use import.meta instead, and five examples with try-it labs.

01

Kind

Read-only

02

Returns

HTMLScriptElement

03

Or

null

04

Scope

Classic scripts

05

Modules

import.meta

06

Status

Baseline widely

Introduction

When a classic (non-module) <script> runs, the browser knows which element is executing. document.currentScript exposes that element so your code can inspect its src, async flag, id, or data-* attributes.

MDN stresses two limits: it does not apply to JavaScript modules (use import.meta), and it does not stay set inside callbacks or event handlers—only while the script is initially being processed.

💡
Capture early

If you need the script element later (inside a click handler or setTimeout), save it at the top of the file: const me = document.currentScript;

Related Document tutorials: cookie, contentType, Document constructor.

Understanding Document.currentScript

A read-only instance property on Document. Its value is an HTMLScriptElement or null.

  • During classic script run — points at that <script> element.
  • Modules — not for type="module"; use import.meta (MDN).
  • Callbacks / handlers — usually null (MDN).
  • Useful reads.src, .async, .defer, .dataset.
  • Not assignable — you cannot set document.currentScript.

📝 Syntax

JavaScript
document.currentScript

Value

An HTMLScriptElement for the classic script currently being processed, or null.

Typical first lines

JavaScript
const scriptEl = document.currentScript;
console.log(scriptEl);           // <script>...</script> or null
console.log(scriptEl?.src);      // URL if external
console.log(scriptEl?.async);    // true / false

⚡ Quick Reference

GoalCode / note
Get running scriptdocument.currentScript
Check async (MDN)document.currentScript.async
Read data attributedocument.currentScript.dataset.config
Keep for laterconst me = document.currentScript;
ES modulesimport.meta.url (not currentScript)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.currentScript.

Type
HTMLScriptElement | null

Or null

Access
read-only

No setter

When
initial run

Not in handlers

Status
baseline

Standard API

📋 Useful HTMLScriptElement properties

PropertyWhat it tells you
.srcURL of an external script (empty for inline)
.asyncWhether the script has the async attribute (MDN example)
.deferWhether execution is deferred until parse finishes
.typeMIME / module type string
.dataset.*Custom data-* options passed via the tag

Examples Gallery

Examples follow MDN Document: currentScript. Use classic (non-module) scripts. Use View Output or Try It Yourself for each case.

📚 Getting Started

See the running script element and its async flag.

Example 1 — Log document.currentScript

While a classic script runs, the property points at that element.

JavaScript
const el = document.currentScript;
console.log("Tag:", el && el.tagName); // "SCRIPT"
console.log("Id:", el && el.id);
console.log("Inline?:", el && !el.src);
Try It Yourself

How It Works

The browser updates currentScript for the duration of top-level classic script evaluation.

Example 2 — Check Async Execution (MDN)

MDN’s example: inspect the async property on the current script.

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

How It Works

External scripts with the async attribute report async === true while they run.

📈 Config, Null & Saved References

Pass options via data attributes and keep a handle for later.

Example 3 — Read data-* Config from the Script Tag

Libraries often take options from attributes on their own <script>.

JavaScript
// <script data-theme="dark" data-api="/v1"> ...
const cfg = document.currentScript.dataset;
console.log("theme:", cfg.theme);
console.log("api:", cfg.api);
Try It Yourself

How It Works

data-theme becomes dataset.theme. Read these values only during initial processing (or from a saved reference).

Example 4 — null Inside a Callback (MDN)

After the script finishes top-level work, handlers usually see null.

JavaScript
console.log("During load:", document.currentScript !== null);

setTimeout(() => {
  console.log("Inside timer:", document.currentScript); // null
}, 0);
Try It Yourself

How It Works

MDN: the property does not reference the element when code runs as a callback or event handler.

Example 5 — Save a Reference for Later

Keep the element so click handlers can still use it.

JavaScript
const me = document.currentScript;

document.getElementById("btn").addEventListener("click", () => {
  console.log("Saved id:", me.id);
  console.log("currentScript now:", document.currentScript); // null
});
Try It Yourself

How It Works

Closing over me is the standard pattern for widgets that configure themselves from their own script tag.

🚀 Common Use Cases

  • Embeddable widgets — read data-* options from the include script.
  • CDN snippets — detect whether the snippet loaded with async.
  • Base URL helpers — derive paths from currentScript.src for classic bundles.
  • Self-removal — some loaders remove their own script element after boot.
  • Not for modules — use import.meta.url in ESM (MDN).
  • Not for “who called me?” in handlers — save a reference early instead.

🧠 How currentScript Is Set

1

Browser starts a classic script

Inline or external non-module <script> begins evaluation.

Start
2

currentScript points at it

Top-level code can read src, async, dataset, and more.

Live
3

Script finishes / callbacks run

Property clears for handlers and timers (MDN).

Clear
4

Your saved reference remains

A variable captured during load still points at the same HTMLScriptElement.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • Returns HTMLScriptElement or null.
  • Not for JavaScript modules — use import.meta (MDN).
  • Null in callbacks / event handlers unless you saved a reference earlier.
  • Related events on Document (advanced): beforescriptexecute, afterscriptexecute (see MDN).
  • Related: cookie, contentType, Document constructor.

Universal Browser Support

Document.currentScript is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

Document.currentScript

Read-only HTMLScriptElement (or null) for the classic script currently being processed.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported from IE 11
Partial support
Document.currentScript Excellent

Bottom line: Use document.currentScript only during classic script evaluation. Capture a reference early; use import.meta in modules.

Conclusion

Document.currentScript tells classic scripts which <script> element is running right now. Use it for config and async checks during initial evaluation, save a reference for later, and switch to import.meta in modules.

Continue with customElementRegistry, cookie, contentType, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Read currentScript at the top of classic scripts
  • Save const me = document.currentScript for callbacks
  • Pass widget options with data-* on the script tag
  • Use import.meta inside ES modules
  • Guard with optional chaining when unsure (?.)

❌ Don’t

  • Expect a value inside click handlers without saving first
  • Rely on it in type="module" scripts
  • Assign to document.currentScript
  • Assume every page script is the “current” one later
  • Confuse it with document.scripts (the full collection)

Key Takeaways

Knowledge Unlocked

Five things to remember about document.currentScript

Classic scripts only — capture early, modules use import.meta.

5
Core concepts
02

Status

baseline

Standard
🔒03

Access

read-only

DOM
⚠️04

Handlers

often null

MDN
📦05

Modules

import.meta

ESM

❓ Frequently Asked Questions

The HTMLScriptElement whose classic (non-module) script is currently being processed, or null if no such script is running.
No. MDN marks Document.currentScript as Baseline Widely available (since July 2015). It is a standard read-only Document instance property.
MDN: it only references the script element while the script is initially being processed. Inside callbacks, timeouts, and event handlers it is usually null.
No. MDN says it does not apply to JavaScript modules. For 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 you put on the <script> tag).
Did you know?

Before document.currentScript was widely available, authors sometimes walked document.getElementsByTagName("script") and guessed the last element was “this” script. That heuristic broke with async and dynamically inserted scripts— currentScript is the reliable modern answer for classic scripts.

Next: customElementRegistry

Learn how to read the document’s CustomElementRegistry (or null).

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.

6 people found this page helpful