const urlParams = new URLSearchParams(window.location.search); const myParam = urlParams.get('myParam'); console.log(myParam); // output: "someValue"
const urlParams = new URLSearchParams(window.location.search); urlParams.set('newParam', 'newValue'); const newUrl = window.location.pathname + '?' + urlParams.toString(); window.location.href = newUrl;
const urlParams = new URLSearchParams(window.location.search); urlParams.delete('myParam'); const newUrl = window.location.pathname + '?' + urlParams.toString(); window.location.href = newUrl;In this example, we use the `.delete()` method to remove the query parameter "myParam" from the current URLSearchParams object. Then, we use the same steps as the previous example to convert the object back to a string format and set the window location href to the new URL. These examples demonstrate the basic usage of URLSearchParams in JavaScript. The package library for this API is included in the built-in JavaScript library and does not require any external packages or dependencies.