1. Introduction
Sometimes there is a need to get request path with full all query params in JavaScript code. The good news is we don't need to have any framework (like jQuery) to achieve that. In a simple vanilla code, we can get all set up parameters from the request.
2. Sample JavaScript function to retrieve query parameters
This simple function presented below: queryParam(queryParameter: string) reads search parameters from URL (window.location.search) and split them by parameter separator '&'.
In b variable you will have map that contains:
- key - request parameter name,
- value - request parameter value.
To get request parameter value simply call queryParam function with single attribute that will be your parameter name.
For URL with parameters like this: ?param1=val1¶m2=query+string, function queryParam will return the following:
3. Conclusion
In this article, we showcased how to get query parameters from JavaScript code. Parsing window.location
attributes are all we need in that case, no additional JS frameworks are required.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}