Home > Web Front-end > JS Tutorial > How Can I Retrieve Query String Parameters in JavaScript?

How Can I Retrieve Query String Parameters in JavaScript?

DDD
Release: 2024-11-27 08:33:14
Original
764 people have browsed it

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;
}
Copy after login

Usage:

var paramValue = getQueryString()["paramName"];
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template