JavaScript: Get Current Script Element

By Xah Lee. Date: . Last updated: .

This is the best way to get the current script tag

Return the current script element. That is, the script element the JavaScript is running from.
const xx = document.currentScript;

supported by all major browsers since 2015. (it's not supported by Internet Explorer version 11)

Alternatives for Older Browser

// get the script element, by matching the script name
const xx = document.querySelector('script[src$="my_script_name.js"]');
JavaScript in Depth

DOM Scripting


DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc