1// example url: https://mydomain.com/?fname=johnny&lname=depp
2const queryString = window.location.search;
3console.log(queryString);
4// ?fname=johnny&lname=depp
5
6const urlParams = new URLSearchParams(queryString);
7
8const firstName = urlParams.get('fname');
9console.log(firstName);
10// johnny
11
12const lastName = urlParams.get('lname');
13console.log(lastName);
14// depp
1const queryString = window.location.search;
2const parameters = new URLSearchParams(queryString);
3const value = parameters.get('key');
1// example url: https://mydomain.com/?fname=johnny&lname=depp
2const urlParams = new URLSearchParams(window.location.search);
3const firstName = urlParams.get('fname'); // johnny
1function getUrlParameter(name) {
2 name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
3 var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
4 var results = regex.exec(location.search);
5 return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
6};
1const getParameters = (URL) => {
2 URL = JSON.parse('{"' + decodeURI(URL.split("?")[1]).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') +'"}');
3 return JSON.stringify(URL);
4};
5
6getParameters(window.location)
7// Result: { search : "easy", page : 3 }
8