Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to read/manipulate query string params using javascript?

The examples I've seen online seem much more complex than I expected (manually parsing &/?/= into pairs, using regular expressions, etc). We're using asp.net ajax (don't see anything in their client side reference) and would consider adding jQuery if it would really help.

I would think there is a more elegant solution out there - so far this is the best code I've found but I would love to find something more along the lines of the HttpRequest.QueryString object (asp.net server side). Thanks in advance,

Shane

like image 610
jskunkle Avatar asked Sep 29 '08 20:09

jskunkle


2 Answers

There is indeed a QueryString plugin for jQuery, if you're willing to install the jQuery core and the plugin it could prove useful.

like image 93
Adam Bellaire Avatar answered Sep 20 '22 17:09

Adam Bellaire


I am using this function in case i don't want to use a plugin:

function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return pair[1];
        }
    }
    return null;
}
like image 27
Amr Elgarhy Avatar answered Sep 19 '22 17:09

Amr Elgarhy