How to get the query string by javascript?

How to extract the query string from the URL in javascript?

Thank you!



You can easily build a dictionary style collection...

function getQueryStrings() { 
  var assoc  = {};
  var decode = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); };
  var queryString =; 
  var keyValues = queryString.split('&'); 

  for(var i in keyValues) { 
    var key = keyValues[i].split('=');
    if (key.length > 1) {
      assoc[decode(key[0])] = decode(key[1]);

  return assoc; 

And use it like this...

var qs = getQueryStrings();
var myParam = qs["myParam"]; 

If you're referring to the URL in the address bar, then

will give you just the query string part. Note that this includes the question mark at the beginning.

If you're referring to any random URL stored in (e.g.) a string, you can get at the query string by taking a substring beginning at the index of the first question mark by doing something like:


That assumes that any question marks in the fragment part of the URL have been properly encoded. If there's a target at the end (i.e., a # followed by the id of a DOM element) it'll include that too.


Here's the method I use...

function Querystring() {
    var q =, qs = {};
    if (q.length) {
        var keys = q.split("&"), k, kv, key, val, v;
        for (k = keys.length; k--; ) {
            kv = keys[k].split("=");
            key = kv[0];
            val = decodeURIComponent(kv[1]);
            if (qs[key] === undefined) {
                qs[key] = val;
            } else {
                v = qs[key];
                if (v.constructor != Array) {
                    qs[key] = [];
    return qs;

It returns an object of strings and arrays and seems to work quite well. (Strings for single keys, arrays for the same key with multiple values.)


You need to simple use following function.

function GetQueryStringByParameter(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(;
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));

--- How to Use ---

var QueryString= GetQueryStringByParameter('QueryString');

Very Straightforward!

function parseQueryString(){
    var assoc = {};
    var keyValues ='&');
    var decode = function(s){
        return decodeURIComponent(s.replace(/\+/g, ' '));

    for (var i = 0; i < keyValues.length; ++i) {
        var key = keyValues[i].split('=');
        if (1 < key.length) {
            assoc[decode(key[0])] = decode(key[1]);

    return assoc;

Works for me-

function querySt(Key) {

    var url = window.location.href;

    KeysValues = url.split(/[\?&]+/); 

    for (i = 0; i < KeysValues.length; i++) {

            KeyValue= KeysValues[i].split("=");

            if (KeyValue[0] == Key) {

                return KeyValue[1];




function GetQString(Key) {     

    if (querySt(Key)) {

         var value = querySt(Key);

         return value;        



You can use this Javascript :

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(;
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));


You can also use the plugin jQuery-URL-Parser allows to retrieve all parts of URL, including anchor, host, etc.

Usage is very simple and cool:


via James&Alfa


I have use this method

function getString()
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
    hash = hashes[i].split('=');
    vars[hash[0]] = hash[1];
return vars;
var buisnessArea = getString();
// Assuming "?post=1234&action=edit"

var urlParams = new URLSearchParams(;

console.log(urlParams.has('post')); // true

console.log(urlParams.get('action')); // "edit"

console.log(urlParams.getAll('action')); // ["edit"]

console.log(urlParams.toString()); // "?post=1234&action=edit"

console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"

There is a new API called URLSearchParams in browsers which allow you to extract and change the values of the query string.

Currently, it seems to be supported in Firefox 44+, Chrome 49+ and Opera 36+.


To get started, create a new URLSearchParams object. For current implementations, you need to remove the "?" at the beginning of the query string, using slice(1) on the querystring, as Jake Archibald suggests:

var params = new URLSearchParams(; // myParam=12

In later implementations, you should be able to use it without slice:

var params = new URLSearchParams(; // myParam=12


You can get params from it via the .get method:

params.get('myParam'); // 12


Params can be changed using .set:

params.set('myParam', 'newValue');


And if the current querystring is needed again, the .toString method provides it:

params.toString(); // myParam=newValue

There are a host of other methods in this API.


As browser support is still pretty thin, there is a small polyfill by Andrea Giammarchi (<3kB).


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.