URLSearchParams handles decoding and repeated keys. Object.fromEntries flattens the iterator into a plain object for the common case where each key appears once.
typescript
export function getUrlParams(url: string = window.location.href) {
const params = new URL(url).searchParams
return Object.fromEntries(params.entries())
}For keys that can repeat, such as checkbox filters, read them individually with getAll instead of the flattened object, which only keeps the last value per key.
typescript
// https://example.com/shop?category=shoes&sort=price&tag=sale&tag=new
getUrlParams()
// { category: 'shoes', sort: 'price', tag: 'new' }
new URL(window.location.href).searchParams.getAll('tag')
// ['sale', 'new']