Javascript Self URL

Say you're loading a javascript file with <script src="https://example.com/webring.js">. How does that script know its own URL and therefore how to refer to a file relative to itself?

Modern browsers support currentScript, but the safer way of doing this currently (2023) is:

const url = document.querySelector('script[src*="webring.js"]').src.replace("webring.js", "")

The above code assumes only one webring.js script, but you could add an ID to the script tag and use that instead if you had to

References