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
Call
What it does
document.currentScript
Primary signature.
Built into the browser. Read or assign document.currentScript — no import required.
Library declarations
JavaScript
document.currentScript
Parameters
Parameter
Description
Classic <script>
Required. Modules are excluded (MDN)
Writable?
Optional. Read-only property
Return Value
Return Value
Description
*
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