Home >Web Front-end >JS Tutorial >How to Retrieve GET Parameter Values in JavaScript?

How to Retrieve GET Parameter Values in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-12-27 00:38:10524browse

How to Retrieve GET Parameter Values in JavaScript?

Retrieving Values from GET Parameters in JavaScript

Retrieving values from GET parameters in JavaScript can be tricky, as the language itself lacks built-in functions for handling such parameters. This can lead to confusion when attempting to extract specific parameter values from URLs.

Solution: Utilizing the URL Object

Fortunately, modern browsers and Node.js implement the URL object, which allows us to interact with and manipulate URLs in a programmatic manner. Here's how we can use the URL object to retrieve the value of the "c" GET parameter:

// Given a URL string containing GET parameters
const urlString = "www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5";

// Create a URL object
const url = new URL(urlString);

// Get the value of the "c" parameter using searchParams
const cValue = url.searchParams.get("c");

// Log the retrieved value
console.log(cValue);

This approach will successfully retrieve the entire value of the "c" parameter, including the hyphenated segments ("m2-m3-m4-m5").

The above is the detailed content of How to Retrieve GET Parameter Values in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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