-2

I have a html page index.html. In the head tag of that page I want to define a script src:

<script src="http://www.example/hrm.js?pId=5"></script>

I want the pId number to be filled in by the parameter I give to my html file. for example if I open .../index.html?pId=6, I want in the script tag of that page

<script src="http://www.example/hrm.js?pId=6"></script>

How can I do that? I tried this: Use JS variable to set the src attribute for <script> tag but the problem is that I have multiple script to load ! and nothting work anymore when I put that piece of code.

Bigjo
  • 613
  • 2
  • 10
  • 32
  • 1
    you could use JavaScript to read the parameter from the querystring (hint: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams), and then write script which itself creates the new script tag, and concatenate the parameter value into the src string. Hint: https://cleverbeagle.com/blog/articles/tutorial-how-to-load-third-party-scripts-dynamically-in-javascript . Did you do any research into this yourself? Or make any attempt at all? We mostly like to help people fix their code, not do all their research and coding for them. – ADyson Feb 13 '19 at 21:04
  • Possible duplicate of [Use JS variable to set the src attribute for – cb64 Feb 13 '19 at 21:08
  • 1
    Possible duplicate of [Use JS variable to set the src attribute for – Luca Kiebel Feb 13 '19 at 21:11

1 Answers1

0

You can use this:

var pId = new URL(document.currentScript.getAttribute('src')).searchParams.get("pId");

references:

Note: according to MDN URL is not supported by IE, see the link to see the browsers that support it, probably there is polyfill. currentScript is also not supported by IE.

jcubic
  • 61,973
  • 54
  • 229
  • 402