Skip to content

Instantly share code, notes, and snippets.

@amorkovin
Created September 25, 2020 11:35
Show Gist options
  • Save amorkovin/69fa0ab02413c25acb90db140c05d0db to your computer and use it in GitHub Desktop.
Save amorkovin/69fa0ab02413c25acb90db140c05d0db to your computer and use it in GitHub Desktop.
Удалят указанный get-параметр из переданного url. JavaScript.
// Удаляет указанный get-параметр из URL.
function removeGetParam(url, paramName) {
const parts = window.location.search.substr(1).split("&");
let $_GET = {};
for (let i = 0; i < parts.length; i++) {
let temp = parts[i].split("=");
$_GET[decodeURIComponent(temp[0])] = decodeURIComponent(temp[1]);
}
const oldUrl = new URL(url);
let newSearch = '';
let i = 1;
for (let prop in $_GET) {
if (prop !== paramName) {
newSearch = newSearch + '&' + prop + '=' + $_GET[prop];
}
i++;
}
if (newSearch) {
newSearch = '?' + newSearch.slice(1);
}
let newPathname = '';
if (oldUrl.pathname.length > 1) {
newPathname = oldUrl.pathname;
}
const newUrl = oldUrl.origin + newPathname + newSearch;
return newUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment