Reputation: 2201
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?
Upvotes: 0
Views: 1317
Reputation: 20778
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.
Upvotes: 0
Reputation: 741
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")
Upvotes: 1
Reputation: 2032
If there's one specific variable you want, you can use the document.location
property and split out the part after id=
Upvotes: 0