Get and Set URL Parameters
Get and Set URL Parameters is a reusable JavaScript snippet. Read, add, and modify URL query string parameters using the URLSearchParams API. The full implementation: // Get params from current URL const params = new URLSearchParams(window.location.search) const page = params.get('page') // "2" const hasFilter = params.has('filter') // true // Set/update params params.set('page', '3') params.delete('filter') const newUrl = `?${params.toString()}` window.history.pushState({}, '', newUrl) // Build params from object function buildQuery(obj) { return '?' + new URLSearchParams(obj).toString() } buildQuery({ page: 1, sort: 'asc' }) // "?page=1&sort=asc" This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.