TIL: Get script URL
2023-08-25
- js
- dom
There’s a really handy little DOM api that gives you the URL for the currently loaded script. It’s document.currentScript.src. MDN docs.
Example when used:
console.log(document.currentScript.src);
// => "http://localhost:3000/_next/static/chunks/src_components_Workspace_tsx.js"
You can then parse that URL with new URL(...) and pluck out all the useful stuff you might need.