xxxxxxxxxx
function setQueryStringParameter(name, value) {
const params = new URLSearchParams(window.location.search);
params.set(name, value);
window.history.replaceState({}, "", decodeURIComponent(`${window.location.pathname}?${params}`));
}
xxxxxxxxxx
// Easy Way - how to add parameters to url javascript
const MyURL = new URLSearchParams(window.location.search);
MyURL.set('Name', 'HazaaZOOZ');
window.location.search = MyURL;
xxxxxxxxxx
var url = new URL('http://demourl.com/path?topic=main');
var search_params = url.searchParams;
search_params.append('id', '101');
search_params.append('id', '102');
url.search = search_params.toString();
var new_url = url.toString();
// output : http://demourl.com/path?id=100&id=101&id=102&topic=main
console.log(new_url);
xxxxxxxxxx
function setUrlParam(param, value) {
const url = new URL(window.location.href);
url.searchParams.set(param, value);
window.history.pushState({}, "", url);
}
function deleteUrlParam(param) {
const url = new URL(window.location.href);
url.searchParams.delete(param);
window.history.pushState({}, "", url);
}