1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/utils/url.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

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 = () => {
// 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;
}
// 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
};