Pass parameters to a Script tag - Get Parameters of Current Script Tag Src
or
Get Parameters of Current Script Tag Src
or
Pass parameters to a Script tag
See an Example Below -
//Script Tag in Main HTML (i.e. index.html)
<script src="script.js?id=WqoYv89"></script>
Here is Content of script.js
//Content of Script.js
let params = new URL(document.currentScript.src);
params = params.searchParams;
console.log(params.get('id'));
Result on Console :- WqoYv89
About UrlSearchParams :- https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get
About Document.currentScript :- https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript
Comments
Post a Comment