Home > Web Front-end > JS Tutorial > How to Extract GET Parameter Values Using JavaScript?

How to Extract GET Parameter Values Using JavaScript?

Patricia Arquette
Release: 2024-12-19 20:16:14
Original
1036 people have browsed it

How to Extract GET Parameter Values Using JavaScript?

Obtaining Values from GET Parameters in JavaScript

Many web applications use GET parameters to pass data to the server. These parameters are appended to the URL in the format of key-value pairs, such as "?parameter1=value1¶meter2=value2". In JavaScript, there are built-in methods for accessing these parameters when working with the URL object.

In the given example, you need to retrieve the value associated with the 'c' parameter from the URL "www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5". Using the JavaScript URL object, you can achieve this as follows:

// Create a URL object with the specified URL string
var url = new URL("www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5");

// Read the value associated with the 'c' parameter
var cValue = url.searchParams.get("c");

// Print the value
console.log(cValue);
Copy after login

The above is the detailed content of How to Extract GET Parameter Values Using 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template