Roadmap to becoming a developer in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.1 KiB

export function getUrlParams() {
if (typeof window === 'undefined') {
return {};
}
const params = new URLSearchParams(window.location.search);
const paramsObj: Record<string, any> = {};
for (const [key, value] of params.entries()) {
paramsObj[key] = value;
}
return paramsObj;
}
export function deleteUrlParam(key: string) {
if (typeof window === 'undefined') {
return;
}
const url = new URL(window.location.href);
if (!url.searchParams.has(key)) {
return;
}
url.searchParams.delete(key);
window.history.pushState(null, '', url.toString());
}
export function setUrlParams(params: Record<string, string>) {
if (typeof window === 'undefined') {
return;
}
const url = new URL(window.location.href);
9 months ago
let hasUpdatedUrl = false;
for (const [key, value] of Object.entries(params)) {
9 months ago
if (url.searchParams.get(key) === String(value)) {
continue;
}
url.searchParams.delete(key);
url.searchParams.set(key, value);
9 months ago
hasUpdatedUrl = true;
}
9 months ago
if (hasUpdatedUrl) {
window.history.pushState(null, '', url.toString());
}
}