Get the values from the "GET" parameters (JavaScript) [duplicate] – Dev

The best answers to the question “Get the values from the "GET" parameters (JavaScript) [duplicate]” in the category Dev.


I have a URL with some GET parameters as follows: 

I need to get the whole value of c. I tried to read the URL, but I got only m2. How do I do this using JavaScript?


Most implementations I’ve seen miss out URL-decoding the names and the values.

Here’s a general utility function that also does proper URL-decoding:

function getQueryParams(qs) {
    qs = qs.split('+').join(' ');

    var params = {},
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);

    return params;

//var query = getQueryParams(;


JavaScript itself has nothing built in for handling query string parameters.

Code running in a (modern) browser you can use the URL object (which is part of the APIs provided by browsers to JS):

var url_string = ""; //window.location.href
var url = new URL(url_string);
var c = url.searchParams.get("c");

For older browsers (including Internet Explorer), you can use this polyfill or the code from the original version of this answer that predates URL:

You could access, which would give you from the ? character on to the end of the URL or the start of the fragment identifier (#foo), whichever comes first.

Then you can parse it with this:

function parse_query_string(query) {
  var vars = query.split("&");
  var query_string = {};
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    var key = decodeURIComponent(pair[0]);
    var value = decodeURIComponent(pair[1]);
    // If first entry with this name
    if (typeof query_string[key] === "undefined") {
      query_string[key] = decodeURIComponent(value);
      // If second entry with this name
    } else if (typeof query_string[key] === "string") {
      var arr = [query_string[key], decodeURIComponent(value)];
      query_string[key] = arr;
      // If third or later entry with this name
    } else {
  return query_string;

var query_string = "a=1&b=3&c=m2-m3-m4-m5";
var parsed_qs = parse_query_string(query_string);

You can get the query string from the URL of the current page with:

var query =;
var qs = parse_query_string(query);


This is an easy way to check just one parameter:

Example URL:


Example Javascript:

var myParam ='myParam=')[1]

if “myParam” exists in the URL… variable myParam will contain “2”, otherwise it will be undefined.

Maybe you want a default value, in that case:

var myParam ='myParam=')[1] ?'myParam=')[1] : 'myDefaultValue';

Update: This works better:

var url = ""; // or window.location.href for current url
var captured = /myParam=([^&]+)/.exec(url)[1]; // Value is in [1] ('384' in our case)
var result = captured ? captured : 'myDefaultValue';

And it works right even when URL is full of parameters.



function gup( name, url ) {
    if (!url) url = location.href;
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( url );
    return results == null ? null : results[1];
gup('q', 'hxxp://')