As an example, I have the following html:
<body>
<script src="code.js?q=xyz"></script>
</body>
I want to be able to read the q=xyz
query parameter from within code.js
. I tried:
console.log(window.location.href)
But it gives me the html document's URL, not code.js
's URL (I could then parse the URL for the query parameters).
Is there any way to get an imported JavaScript file's query parameters from within that JavaScript? I know I could read the query parameters on a server and then send back JavaScript that has the query parameters embedded, but I don't want that. I want to be able to put JavaScript files on CDNs that can be configured via query parameters.
document.currentScript
– Mannes