xxxxxxxxxx
// Example URL: https://example.com/over/there?name=ferret
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const parameter_name = urlParams.get('name')
console.log(parameter_name);
xxxxxxxxxx
// https://testsite.com/users?page=10&pagesize=25&order=asc
const urlParams = new URLSearchParams(window.location.search);
const pageSize = urlParams.get('pageSize');
xxxxxxxxxx
const params = new URLSearchParams(window.location.search);
// Check if we have the param
if (params.has("myParam")) {
console.log("Yep! We have it. Value is: " + params.get("myParam"));
} else {
console.log("The param myParam is not present.");
}
xxxxxxxxxx
const url = new URL(window.location.href);
const parameterValue = url.searchParams.get('pramaName');
console.log(parameterValue);
// another method
let url = "https://example.com?param1=value1¶m2=value2";
let params = new URLSearchParams(url);
console.log(params.get("param1")); // "value1"
console.log(params.get("param2")); // "value2"
xxxxxxxxxx
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const code = urlParams.get('code')
xxxxxxxxxx
const params = new URLSearchParams(window.location.search);
const something = decodeURIComponent(params.get('hi'));
// or shorter, no need to use variables
decodeURIComponent((new URLSearchParams(window.location.search)).get('hi'))
xxxxxxxxxx
// EXAMPLE URL: http://localhost:5500/feed?id=4567825&search=kittens
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const id = urlParams.get('id'); // result: 4567825
const search = urlParams.get("search") // result: "kittens"
xxxxxxxxxx
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const page_type = urlParams.get('page_type')
console.log(page_type);
xxxxxxxxxx
// Get the current URL
const url = new URL(window.location.href);
// Get the search parameters from the URL
const params = new URLSearchParams(url.search);
// Get the value of a specific parameter
const parameterValue = params.get('parameterName');
xxxxxxxxxx
// Get URL parameters using URLSearchParams
const params = new URLSearchParams(window.location.search);
const paramValue = params.get('paramName');
console.log(paramValue); // The value of 'paramName' parameter
// Set URL parameter
params.set('anotherParam', 'exampleValue');
const newUrl = window.location.origin + window.location.pathname + '?' + params.toString();
console.log(newUrl); // New URL with the added parameter