0

What is the best way to pass a value from C# code to javascript? Currently I am setting an asp.net hidden field in the Page_Load method.

Also if I pass a value using GET like

Response.Redirect("myurl.com/myPage.aspx?id=300");

how can I get the value of id from myPage using javascript?

Is there a nice way to do this in jquery?

Arizona1911
  • 2,181
  • 9
  • 29
  • 38
  • 1
    To extract the values of the querystring from the URL, you may want to check out [this solution](http://stackoverflow.com/questions/647259/javascript-query-string/647272#647272) by [@CMS](http://stackoverflow.com/users/5445/cms) in another Stack Overflow post. – Daniel Vassallo Sep 12 '10 at 19:49
  • This is a good solution. I would give you a checkmark if I could. – Arizona1911 Sep 12 '10 at 20:02

3 Answers3

1
function getParameter(name)
{
    name = name.replace(/[[]/,"\[").replace(/[]]/,"\]");
    var results = new RegExp("[\?&]" + name + "=([^&#]*)").exec(window.location.href);

    return (results != null ? results[1] : "");
}

Use the following code to get your parameter: getParameter("id")

BlueCode
  • 741
  • 5
  • 12
0

If there's one specific variable you want, you can use the document.location property and split out the part after id=

Tim Green
  • 2,028
  • 1
  • 17
  • 19
0

Tim's idea is good. You could also directly insert the values into the javascript with something like var idValue = '<%= SomeProtectedProperty %>'; within your script. That's for if you know it at load time.

Jason Kleban
  • 20,024
  • 18
  • 75
  • 125