Retrieving Values from URL Parameters in JavaScript
When dealing with URLs that contain query string parameters (like www.test.com/t.html?a=1&b=3&c=m2-m3-m4-m5), accessing and manipulating these parameters is often necessary. However, JavaScript doesn't inherently provide built-in capabilities for handling query strings.
To overcome this, browsers and Node.js leverage the URL object to parse and work with these parameters. By creating a URL instance from the given URL string, you can access individual parameters using the searchParams property.
var url_string = "http://www.example.com/t.html?a=1&b=3&c=m2-m3-m4-m5"; var url = new URL(url_string); var c = url.searchParams.get("c");
The get() method of searchParams takes the parameter name ("c" in this example) and returns its associated value. This allows you to retrieve the entire value of parameter c, even if it contains multiple segments like in the provided example.
The above is the detailed content of How Can I Retrieve Values from URL Parameters in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!