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

6
  • To get the querystring part of URL you need document.location.search. If you want to pull the parameter from URL look at stackoverflow.com/questions/19491336/get-url-parameter-jquery Commented Jun 10, 2015 at 18:13
  • @tabz100 My mistake. I just updated the question. Please check Commented Jun 10, 2015 at 18:15
  • Do you want to access id=2 in the widget.js file or the current page? Commented Jun 10, 2015 at 18:22
  • id=2 It is a widget code. Commented Jun 10, 2015 at 18:23
  • Why did you delete your answer? It worked!!! Commented Jun 10, 2015 at 18:36

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Where will I write this code? Do remember that the file where widget code block written will not be in my control
If you have no control over the widget code then how is the widget code using the parameters?
I mean I don't have controller of caller of widget.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.