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
Fundamentals
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;
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.
Foundation
📝 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
Compare
⚖️ Classic scripts vs modules
Situation
Use
Notes
Classic <script> / <script src>
document.currentScript
While initially processing
<script type="module">
import.meta
currentScript is not for modules (MDN)
Inside onclick / timer
Saved reference
currentScript is usually null
Find any script by id
document.getElementById
Does not mean “currently running”
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Get running script
document.currentScript
Check async (MDN)
document.currentScript.async
Read data attribute
document.currentScript.dataset.config
Keep for later
const me = document.currentScript;
ES modules
import.meta.url (not currentScript)
MDN status
Baseline Widely available
Snapshot
🔍 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
Compare
📋 Useful HTMLScriptElement properties
Property
What it tells you
.src
URL of an external script (empty for inline)
.async
Whether the script has the async attribute (MDN example)
.defer
Whether execution is deferred until parse finishes
.type
MIME / module type string
.dataset.*
Custom data-* options passed via the tag
Hands-On
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);
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported from IE 11
Partial support
Document.currentScriptExcellent
Bottom line: Use document.currentScript only during classic script evaluation. Capture a reference early; use import.meta in modules.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.currentScript
Classic scripts only — capture early, modules use import.meta.
5
Core concepts
💻01
Returns
script element
API
✓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.