Update Query Parameter in URL
Updates a query parameter in the browser's current URL without reloading the page.
updateQueryParam
Updates a query parameter in the browser's current URL without reloading the page, using the History API.
Function Signature
function updateQueryParam(key: string, value: string): voidParameters
| Parameter | Type | Description |
|---|---|---|
key | string | The query parameter key to update or add. |
value | string | The value to set for the key. |
Returns
void: This function does not return a value.
Example Usage
import { updateQueryParam } from 'toolbox-x/dom';
// Assuming current URL: https://example.com/?user=alex&role=admin
updateQueryParam('user', 'sam');
// Browser URL is now: https://example.com/?user=sam&role=adminBehavior Details
- No Reload: Uses
window.history.replaceStateunder the hood. The URL changes visually and programmatically in the browser, but the page does not reload. - State Replacement: Because it uses
replaceState, it updates the current browser history entry rather than pushing a new one, keeping the back button history unchanged. - Browser Only: Depends on
windowand the History API; it will fail if executed on the server.
Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)
