0

I see many widgets that allow this :

<script src="http://www.mywebsite.it/widget/widget.js?year=2000" type="text/javascript"></script>

How can I manage that year? I mean, widget.js, on loading, does an ajax call to an aspx page. And I'd like to pass that year to the aspx page.

How can I do it from the .js?

11
  • what's the problem here? Why can't you make a simple, plain, vanilla GET request to mywebsite.it/widget/widget.js?year=2000 ? Commented May 29, 2012 at 14:49
  • 2
    Check this other question stackoverflow.com/questions/4716612/… Commented May 29, 2012 at 14:50
  • See: stackoverflow.com/questions/979975/… Commented May 29, 2012 at 14:50
  • year is dinamic! If I call the widget with year=2000 or year=2001 it make different results. So, on widget.js, I must do 2 different ajax call to the aspx. Commented May 29, 2012 at 14:51
  • Claudio Redi's link is a good one. If you can add an id attribute to the tag, you can make things slightly simpler (and more robust, on the chance that not all browsers order scripts the obvious way). Commented May 29, 2012 at 14:52

1 Answer 1

2

Give your script an id, say "scr1".

<script id="src1" src="http://www.mywebsite.it/widget/widget.js?year=2000" type="text/javascript"></script>

Then you can do:

var myScript = document.getElementById('scr1');
var src=myScript.src;
var p=/[?&]year=(\d+)/
var r=p.exec(src);
// r[1] contains the year

HTH

Sign up to request clarification or add additional context in comments.

Comments

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.