JS: Get Current Script Element

By Xah Lee. Date: . Last updated: .

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;

Alternatives for Older Browsers Before 2015

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