1

I have used this tutorial to create a JS based widget. One thing I need to do is to pass query string parameters in JS file. I tried document.location.href but it gave the URL of page where widget was placed(which is quite obvious)

Code is given below:

<script src="http://example.com/widget.js?id=2" type="text/javascript"></script>
    <div id="widget"></div>

I need to fetch id=2 which I can pass further.

Thanks

Community
  • 1
  • 1
Volatil3
  • 14,253
  • 38
  • 134
  • 263

1 Answers1

1

If you give your script an id then you can write:

<script src="http://example.com/widget.js?id=2" id="myscript" type="text/javascript"></script>
<div id="widget"></div>

<script>
var myScript = document.getElementById('myscript');
var src= myScript.getAttribute('src');
//Get the id from the src based on parameter using Regular Expression
</script>
tabz100
  • 1,463
  • 11
  • 14