How to get query string variables using JavaScript by Andy E. I looked it over a bit and did some testing and I think this is a very elegant solution.
http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/2880929#2880929
It returns all the variables in the query string with their values in an object allowing access like:
paramValues["param1"], which would return the value of the parameter.
I ran across it when trying to figure out how to use jQuery to return a string from a XML file of constant strings to display a message. JavaScript would be used to look at the query string and if the messageId parameter I needed existed, it would call a server method and pass it the messageId from the query string. Then the server method would read the XML file and return the string. I never got it fully working and I’ll work on it down the road but for now I’m just going to inject the message into my HTML while it’s still on the server and leave it at that. I know I can just read the XML directly using the AJAX method but I need this functionality to extend to a database because that info won’t be stored in the XML file forever. Still, even though I’m not using this query string parser at this point, I’m going to definitely use it. Definitely.
Thanks Andy!
Here’s his website: http://whattheheadsaid.com
Update: I ended up getting this working after all. The problem I was having was making my jQuery.ajax call work nicely with my C# code. I didn’t want to create a seperate HttpHandler because the data was all being supplied by my page’s code behind, so I just wanted to get it from there. Turns out using AJAX to access the page’s functions is not a very straightforward process. After a lot of searching I ended up sending a variable with my AJAX request called ajaxId and gave it a number 1 (in case I have to make another AJAX call later, which would be 2). Then, in my Page_Load event handler I checked the query string for the variable and its value. If found, I do the necessary work to get the data I need and return as a JSON formatted string as Response.Write(). A few things I didn’t figure out for a while were the apparent inability to access a method other than the Page_Load event handler to avoid the messy logic to test for the ajaxId, and the need to call Response.End() after my Write call to keep the rest of the markup of the page from being returned to my AJAX call. Also, since I’m only sending a list of variables to the server with my AJAX call, I made this a $.get rather than a $.post, hence the checking of Request.QueryString for my variables instead of Request.Form. And lastly, since I’m using C#’s JavaScriptSerializer class to serialize the data for JSON usage before spitting it to my output, my $.get method call on the client needed to have a data type of “json” as the last param. This job had gotchas at every turn; but now I know! I’m not really sure the JSON serialization for the returned data was necessary since I am only outputting a string, but it’s surely good practice. Besides, I have used it to return an array before and who knows if this call will have to return more complex data down the road.
