415

I need to parse the query string www.mysite.com/default.aspx?dest=aboutus.aspx. How do I get the dest variable in JavaScript?

13
  • Have a look at this solution. Using his function, you would just not to call gup('dest') to grab the URL dest parameter. Commented Jun 12, 2010 at 3:29
  • function qs(search_for) { var query = window.location.search.substring(1); var parms = query.split('&'); for (var i = 0; i < parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0 && search_for == parms[i].substring(0, pos)) { return parms[i].substring(pos + 1);; } } return ""; } //using : document.write(qs("isFolderLevel")); Commented Jan 21, 2015 at 8:10
  • codepen.io/iegik/pen/NGRzoY?editors=101 Commented Sep 25, 2015 at 12:31
  • 39
    There's already a (non IE) function to do this in native javascript, no need to re-invent the wheel: developer.mozilla.org/en-US/docs/Web/API/URLSearchParams Commented Feb 11, 2019 at 2:31
  • 1
    Simple solution is var url = "www.mysite.com/default.aspx?dest=aboutus.aspx" var query = new URLSearchParams(url.split("?")[1]) query.get('dest') Commented Nov 3, 2020 at 6:55

11 Answers 11

395

Here is a fast and easy way of parsing query strings in JavaScript:

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 (decodeURIComponent(pair[0]) == variable) {
            return decodeURIComponent(pair[1]);
        }
    }
    console.log('Query variable %s not found', variable);
}

Now make a request to page.html?x=Hello:

console.log(getQueryVariable('x'));
Sign up to request clarification or add additional context in comments.

17 Comments

you should also decode any special characters that have been percent-encoded
Easy, but not very fast if you need to get more than one query value. I think a better abstraction is to return a JS object with all the name value pairs from the query string
Also, the split("=") is not good enough, you can have a "name=value" pair where value contains a non-encoded '='. The solution to that is to do an indexOf('=') to find the first '=', and the substring before and after.
what about ?this=1&this=2&this=3
@gotofritz, I don't think that does the same thing: "a=b=c".split("=", 2) gives you [ 'a', 'b' ], what you'd want instead is ['a', 'b=c']
|
205
function parseQuery(queryString) {
    var query = {};
    var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
    for (var i = 0; i < pairs.length; i++) {
        var pair = pairs[i].split('=');
        query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
    }
    return query;
}

Turns query string like hello=1&another=2 into object {hello: 1, another: 2}. From there, it's easy to extract the variable you need.

That said, it does not deal with array cases such as "hello=1&hello=2&hello=3". To work with this, you must check whether a property of the object you make exists before adding to it, and turn the value of it into an array, pushing any additional bits.

10 Comments

if b is an array of one element then this function will fail. ex. somesite.com/?varrible1=data&varrible2= ex. somesite.com/?varrible1=data&varrible
Here are Jasmine tests for this: gist.github.com/amyboyd/68a86fe3f65a77fcfc7f
Fixed bugs and updated the code. Sorry, I want to shout it out loud here: "Javascript, why we have to do this manually? Making mistakes! Isn't JS intended to work in browser and help people in web development?
This works, just add an if statement to validate pair[0]!="".
Could be a bit shortened: queryString.replace(/^\?/, '').split('&') Thanks for the solution :)
|
56

You can also use the excellent URI.js library by Rodney Rehm. Here's how:-

var qs = URI('www.mysite.com/default.aspx?dest=aboutus.aspx').query(true); // == { dest : 'aboutus.aspx' }
    alert(qs.dest); // == aboutus.aspx

And to parse the query string of current page:-

var $_GET = URI(document.URL).query(true); // ala PHP
    alert($_GET['dest']); // == aboutus.aspx 

7 Comments

What does the argument in .query(true) part do? Is it to return the query as a key-value object instead of the query-string?
Cool, but solutions requiring 3rd party libraries aren't ideal. I'm somewhat surprised this solution has received so many upvotes. No offense intended to @SalmanPK
@Madbreaks But custom, re-inventing the wheel, not battle-tested and very limited functionality solutions are? Interesting ;)
A good native solution is (almost) always better than a good solution requiring a 3rd party tool, is all I'm saying.
Even so, it's always nice to know such a tool exists. In fact, I know exactly how to parse a query by hand, but I preferred to Google around for some better solution, and that's how I got here, in the first place. ;)
|
26

Me too! http://jsfiddle.net/drzaus/8EE8k/

(Note: without fancy nested or duplicate checking)

deparam = (function(d,x,params,p,i,j) {
return function (qs) {
    // start bucket; can't cheat by setting it in scope declaration or it overwrites
    params = {};
    // remove preceding non-querystring, correct spaces, and split
    qs = qs.substring(qs.indexOf('?')+1).replace(x,' ').split('&');
    // march and parse
    for (i = qs.length; i > 0;) {
        p = qs[--i];
        // allow equals in value
        j = p.indexOf('=');
        // what if no val?
        if(j === -1) params[d(p)] = undefined;
        else params[d(p.substring(0,j))] = d(p.substring(j+1));
    }

    return params;
};//--  fn  deparam
})(decodeURIComponent, /\+/g);

And tests:

var tests = {};
tests["simple params"] = "ID=2&first=1&second=b";
tests["full url"] = "http://blah.com/?third=c&fourth=d&fifth=e";
tests['just ?'] = '?animal=bear&fruit=apple&building=Empire State Building&spaces=these+are+pluses';
tests['with equals'] = 'foo=bar&baz=quux&equals=with=extra=equals&grault=garply';
tests['no value'] = 'foo=bar&baz=&qux=quux';
tests['value omit'] = 'foo=bar&baz&qux=quux';

var $output = document.getElementById('output');
function output(msg) {
    msg = Array.prototype.slice.call(arguments, 0).join("\n");
    if($output) $output.innerHTML += "\n" + msg + "\n";
    else console.log(msg);
}
var results = {}; // save results, so we can confirm we're not incorrectly referencing
$.each(tests, function(msg, test) {
    var q = deparam(test);
    results[msg] = q;
    output(msg, test, JSON.stringify(q), $.param(q));
    output('-------------------');
});

output('=== confirming results non-overwrite ===');
$.each(results, function(msg, result) {
    output(msg, JSON.stringify(result));
    output('-------------------');
});

Results in:

simple params
ID=2&first=1&second=b
{"second":"b","first":"1","ID":"2"}
second=b&first=1&ID=2
-------------------
full url
http://blah.com/?third=c&fourth=d&fifth=e
{"fifth":"e","fourth":"d","third":"c"}
fifth=e&fourth=d&third=c
-------------------
just ?
?animal=bear&fruit=apple&building=Empire State Building&spaces=these+are+pluses
{"spaces":"these are pluses","building":"Empire State Building","fruit":"apple","animal":"bear"}
spaces=these%20are%20pluses&building=Empire%20State%20Building&fruit=apple&animal=bear
-------------------
with equals
foo=bar&baz=quux&equals=with=extra=equals&grault=garply
{"grault":"garply","equals":"with=extra=equals","baz":"quux","foo":"bar"}
grault=garply&equals=with%3Dextra%3Dequals&baz=quux&foo=bar
-------------------
no value
foo=bar&baz=&qux=quux
{"qux":"quux","baz":"","foo":"bar"}
qux=quux&baz=&foo=bar
-------------------
value omit
foo=bar&baz&qux=quux
{"qux":"quux","foo":"bar"}   <-- it's there, i swear!
qux=quux&baz=&foo=bar        <-- ...see, jQuery found it
-------------------

9 Comments

just tryin' to keep it simple
What if one of the variables in query string includes = (equal) sign
Fails if any query string value does not have an equal sign, for example: '?val1=1&val2&val3=4', because the split on '=' results in pair[1] == null, which decodeURIComponent(null) returns the string "null" instead of value null. Better to use d(pair[1] || '').
@drzaus I used your code, but i needed duplicating parameters to be parsed as array. In case somebody have same needs, - jsfiddle.net/sergiyok/yywhxsqz
there, 7+ years later is (almost) everyone happy?
|
18

Here's my version based loosely on Braceyard's version above but parsing into a 'dictionary' and support for search args without '='. In use it in my JQuery $(document).ready() function. The arguments are stored as key/value pairs in argsParsed, which you might want to save somewhere...

var args = document.location.search.substring(1).split('&');

var argsParsed = {};

for (i=0; i < args.length; i++)
{
    var arg = decodeURIComponent(args[i]);
    
    if (arg.indexOf('=') == -1)
    {
        argsParsed[arg.trim()] = true;
    }
    else
    {
        var kvp = arg.split('=');
        argsParsed[kvp[0].trim()] = kvp[1].trim();
    }
}

8 Comments

Is there a reason for using unescape() instead of decodeURI()?
I would add if(args[i].length){ as the first line in the loop in order to avoid empty keys in argsParsed.
@ghigo Yes, checking for a zero length key would be a good idea, perhaps after trimming though. There was no reason to use unescape(), I can't remember where I copied it from ;-)
Warning: unescape is deprecated. See: developer.mozilla.org/en-US/docs/JavaScript/Guide/…
Don't use this code, it's just wrong. Trimming modifies data, unescape is used instead of decodeURIComponent and it's called in the wrong place (name and value should be parsed separately, not as a part of the name=value string). It also leaks global variables and uses '==' for comparing values.
|
14

Following on from my comment to the answer @bobby posted, here is the code I would use:

    function parseQuery(str)
        {
        if(typeof str != "string" || str.length == 0) return {};
        var s = str.split("&");
        var s_length = s.length;
        var bit, query = {}, first, second;
        for(var i = 0; i < s_length; i++)
            {
            bit = s[i].split("=");
            first = decodeURIComponent(bit[0]);
            if(first.length == 0) continue;
            second = decodeURIComponent(bit[1]);
            if(typeof query[first] == "undefined") query[first] = second;
            else if(query[first] instanceof Array) query[first].push(second);
            else query[first] = [query[first], second]; 
            }
        return query;
        }

This code takes in the querystring provided (as 'str') and returns an object. The string is split on all occurances of &, resulting in an array. the array is then travsersed and each item in it is split by "=". This results in sub arrays wherein the 0th element is the parameter and the 1st element is the value (or undefined if no = sign). These are mapped to object properties, so for example the string "hello=1&another=2&something" is turned into:

{
hello: "1",
another: "2",
something: undefined
}

In addition, this code notices repeating reoccurances such as "hello=1&hello=2" and converts the result into an array, eg:

{
hello: ["1", "2"]
}

You'll also notice it deals with cases in whih the = sign is not used. It also ignores if there is an equal sign straight after an & symbol.

A bit overkill for the original question, but a reusable solution if you ever need to work with querystrings in javascript :)

Comments

10

If you know that you will only have that one querystring variable you can simply do:

var dest = location.search.replace(/^.*?\=/, '');

3 Comments

Not bad. I'd add this so it is unencoded properly: var dest = unescape(location.search.replace(/^.*\=/, '')).replace(/\+/g, " ");
Can you modify this to account for a potential anchor on the query string?
The regex should have a ? after the *. As is, it will fail for a query string of ?dest=foo=bar.
7

The following function will parse the search string with a regular expression, cache the result and return the value of the requested variable:

window.getSearch = function(variable) {
  var parsedSearch;
  parsedSearch = window.parsedSearch || (function() {
    var match, re, ret;
    re = /\??(.*?)=([^\&]*)&?/gi;
    ret = {};
    while (match = re.exec(document.location.search)) {
      ret[match[1]] = match[2];
    }
    return window.parsedSearch = ret;
  })();
  return parsedSearch[variable];
};

You can either call it once without any parameters and work with the window.parsedSearch object, or call getSearch subsequently. I haven't fully tested this, the regular expression might still need some tweaking...

4 Comments

seems like a case of "I have a problem. I'll use some regex to solve it. Now I have two problems." Tell me how this is better than @Braveyard's string parsing method.
Like I wrote, it will be parsed once and cached in an object. @Braveyard's code will parse the whole hash each time you call the function, and loop through all available variables until the correct one is found.
@cori regular expressions vs splitting strings is just a matter of taste I guess...
@cori It is better as it is challenging... Though it is a more frustrated programmer's approach..
5

How about this?

function getQueryVar(varName){
    // Grab and unescape the query string - appending an '&' keeps the RegExp simple
    // for the sake of this example.
    var queryStr = unescape(window.location.search) + '&';

    // Dynamic replacement RegExp
    var regex = new RegExp('.*?[&\\?]' + varName + '=(.*?)&.*');

    // Apply RegExp to the query string
    var val = queryStr.replace(regex, "$1");

    // If the string is the same, we didn't find a match - return false
    return val == queryStr ? false : val;
}

..then just call it with:

alert('Var "dest" = ' + getQueryVar('dest'));

Cheers

2 Comments

Downvoter, would appreciate an explanation...
You should first split at & and then unescape. Otherwise, this code surely fails if the value contains an encoded & or =, especially if it repeats parts of the keyword
4

I wanted a simple function that took a URL as an input and returned a map of the query params. If I were to improve this function, I would support the standard for array data in the URL, and or nested variables.

This should work back and for with the jQuery.param( qparams ) function.

function getQueryParams(url){
    var qparams = {},
        parts = (url||'').split('?'),
        qparts, qpart,
        i=0;

    if(parts.length <= 1 ){
        return qparams;
    }else{
        qparts = parts[1].split('&');
        for(i in qparts){

            qpart = qparts[i].split('=');
            qparams[decodeURIComponent(qpart[0])] = 
                           decodeURIComponent(qpart[1] || '');
        }
    }

    return qparams;
};

7 Comments

if(parts.length <= 1 ){ created bit of confusion...
breaks when ?a=b=c
@Spongman In what situations is ?a=b=c used?
@ShannonMatthews if you want to pass the string "b=c" to the parameter "a". or any value containing the '=' character. the above code assumes the value does not contain a '='.
technically only the key value pair is allowed to use the '=' sign un-escaped. ( = %3D) w3schools.com/tags/ref_urlencode.ASP URL Reserved Characters tools.ietf.org/html/rfc3986#section-2.2 URL Path Parameters tools.ietf.org/html/rfc3986#section-3.3 so the real question is should your code work with URLs that are not in spec, that might be a requirement for your project, but usually it's not unless you have a special case.
|
1

I wanted to pick up specific links within a DOM element on a page, send those users to a redirect page on a timer and then pass them onto the original clicked URL. This is how I did it using regular javascript incorporating one of the methods above.

Page with links: Head

  function replaceLinks() {   
var content = document.getElementById('mainContent');
            var nodes = content.getElementsByTagName('a');
        for (var i = 0; i < document.getElementsByTagName('a').length; i++) {
            {
                href = nodes[i].href;
                if (href.indexOf("thisurl.com") != -1) {

                    nodes[i].href="http://www.thisurl.com/redirect.aspx" + "?url=" + nodes[i];
                    nodes[i].target="_blank";

                }
            }
    }
}

Body

<body onload="replaceLinks()">

Redirect page Head

   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 (decodeURIComponent(pair[0]) == variable) {
                return decodeURIComponent(pair[1]);
            }
        }
        console.log('Query variable %s not found', variable);
    }
    function delayer(){
        window.location = getQueryVariable('url')
    }

Body

<body onload="setTimeout('delayer()', 1000)">

2 Comments

Welcome to Stack Overflow. Is this a Question?
Nope, I thought it might be useful to share an example of the parser in action.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.