JS: Get Current Script Element
This is the best way to get the current script tag
document.currentScript
-
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"]');