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?

2
  • 1
    To extract the values of the querystring from the URL, you may want to check out this solution by @CMS in another Stack Overflow post. Commented Sep 12, 2010 at 19:49
  • This is a good solution. I would give you a checkmark if I could. Commented Sep 12, 2010 at 20:02

3 Answers 3

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")

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

Comments

0

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.