Is there a simple and reliable way to determine the URL of the currently-executing JavaScript file (inside a web page)?
My only thought on this is to scan the DOM for all the script src
attributes to find how the current file was referenced and then figure out the absolute URL by applying it to document.location
. Anyone have other ideas, is there some super-easy method I completely overlooked?
UPDATE: Script elements accessed via the DOM already have a src
property which contains the full URL. I don't know how ubiquitous/standard that is, but alternatively you can use getAttribute("src")
which will return whatever raw attribute value is in the [X]HTML.