Pass parameters to a Script tag - Get Parameters of Current Script Tag Src

Pass parameters to a Script tag - Get Parameters of Current Script Tag Src


Let This is Script Tag in your main html and you have to get the value of id parameter in script.js 
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


google tag manager,how to get a variable from another script javascript,script tag,how to access a variable from another html script,script tag api,where should the script tag go,how to access another script in javascript,how to access variables from another script,how to call method from another script,script tag in body vs head,transform php variable to javascript in laravel 5,laravel passing php var to jquery,url parameters,where should the script tag be placed in html,route parameters



Comments