js parse url params code example
Example 1: javascript read query parameters
const queryString = window.location.search;
console.log(queryString);
const urlParams = new URLSearchParams(queryString);
const firstName = urlParams.get('fname');
console.log(firstName);
const lastName = urlParams.get('lname');
console.log(lastName);
Example 2: js get url parameter
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5";
var url = new URL(url_string);
var c = url.searchParams.get("c");
console.log(c);
Example 3: js get url parameter
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code')
Example 4: javascript get query parameter
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};