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.
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
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With