2022-04-07 16:06:26 -04:00
|
|
|
/**
|
|
|
|
* Gets the url search string.
|
|
|
|
* This function should be used instead of location.search alone, because the app router
|
|
|
|
* includes search parameters in the hash portion of the url.
|
|
|
|
* @returns The url search string.
|
|
|
|
*/
|
|
|
|
export const getLocationSearch = () => {
|
2024-06-25 10:47:00 -04:00
|
|
|
// Check location.hash for a search string (this should be the case for our routing library)
|
|
|
|
let index = window.location.hash.indexOf('?');
|
|
|
|
if (index !== -1) {
|
|
|
|
return window.location.hash.substring(index);
|
|
|
|
}
|
|
|
|
|
2022-04-07 16:06:26 -04:00
|
|
|
// Return location.search if it exists
|
|
|
|
if (window.location.search) {
|
|
|
|
return window.location.search;
|
|
|
|
}
|
|
|
|
|
2024-06-25 10:47:00 -04:00
|
|
|
// Fallback to checking the entire url
|
|
|
|
index = window.location.href.indexOf('?');
|
2022-04-07 16:06:26 -04:00
|
|
|
if (index !== -1) {
|
|
|
|
return window.location.href.substring(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value of a url search parameter by name.
|
|
|
|
* @param name The parameter name.
|
|
|
|
* @param url The url to search (optional).
|
|
|
|
* @returns The parameter value.
|
|
|
|
*/
|
2022-04-05 15:58:12 -04:00
|
|
|
export const getParameterByName = (name: string, url?: string | null | undefined) => {
|
2022-04-06 17:31:54 -04:00
|
|
|
if (!url) {
|
2022-04-07 16:06:26 -04:00
|
|
|
url = getLocationSearch();
|
2022-04-05 15:58:12 -04:00
|
|
|
}
|
|
|
|
|
2022-04-06 17:31:54 -04:00
|
|
|
// eslint-disable-next-line compat/compat
|
|
|
|
return new URLSearchParams(url).get(name) || '';
|
2022-04-05 15:58:12 -04:00
|
|
|
};
|