Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all Query string values using JavaScript [duplicate]

I want to get the values of all parameters of query string from URL.

Example: www.xyz.com?author=bloggingdeveloper&a1=hello

I want to get values of author and a1 parameters using JavaScript.

like image 540
amit Avatar asked Oct 11 '11 07:10

amit


2 Answers

The simplest way to do so is:-

const search = /*your search query ex:-?author=bloggingdeveloper&a1=hello*/
const params = new URLSearchParams(search);
let paramObj = {};
for(var value of params.keys()) {
     paramObj[value] = params.get(value);
 }

if you console paramObj you will get :-

{
author: bloggingdeveloper,
a1: hello
}

simple!!

refer:- https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/keys

like image 179
Ayesha Mundu Avatar answered Nov 11 '22 01:11

Ayesha Mundu


function getUrlVars()
{
    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.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var author = getUrlVars()["author"];
var a1 = getUrlVars()["a1"];
like image 30
TimT Avatar answered Nov 10 '22 23:11

TimT