Created
September 25, 2020 11:35
-
-
Save amorkovin/69fa0ab02413c25acb90db140c05d0db to your computer and use it in GitHub Desktop.
Удалят указанный get-параметр из переданного url. JavaScript.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Удаляет указанный 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