You can retrieve query string parameters from the current URL using the URLSearchParams API, which provides a convenient way to work with the query string of a URL. Here's how you can use it:
Access the Query String:
Use window.location.search to get the query string, which includes the ? character and all parameters.
const queryString = window.location.search;
Create a URLSearchParams Object:
Instantiate a URLSearchParams object with the query string.
const urlParams = new URLSearchParams(queryString);
Retrieve Specific Parameters:
Use the .get() method to retrieve the value of a specific parameter by name.
const paramValue = urlParams.get('paramName');
Example:
Suppose the current URL is https://example.com/?user=JohnDoe&age=25.
const queryString = window.location.search; // "?user=JohnDoe&age=25"
const urlParams = new URLSearchParams(queryString);
const user = urlParams.get('user'); // "JohnDoe"
const age = urlParams.get('age'); // "25"