Home >Web Front-end >JS Tutorial >How Can I Retrieve Query String Parameters in JavaScript?
JavaScript Query String Retrieval
In JavaScript, the location.search property holds the portion of the URL that follows the '?' symbol. This section contains query parameters and their associated values, similar to the concept of a dictionary in ASP.NET.
To create a key-value dictionary from the query string, you can use the following function:
function getQueryString() { var result = {}, queryString = location.search.slice(1), re = /([^&;]+)=([^&;]*)/g, m; while (m = re.exec(queryString)) { result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); } return result; }
Usage:
var paramValue = getQueryString()["paramName"];
Note: Some JavaScript libraries may provide their own methods for parsing the query string. However, the provided function is a straightforward and reliable solution that can be used in most situations.
The above is the detailed content of How Can I Retrieve Query String Parameters in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!