Reputation: 85
I'm trying to query the pid
parameter of my http://localhost:3000/edit-product?pid=Z4HLrHGZ1ikKIwlEVkM6
URL.
Here is my code:
const params = new URLSearchParams(window.location.path);
const pid = params.get("pid");
console.log("This----->", pid);
Each time I run the code, the console prints that the pid value is null
Any ideas what I'm doing wrong?
Upvotes: 0
Views: 80
Reputation: 9807
You should be using window.location.search
. window.location.path
is undefined
.
Gotchas
The URLSearchParams constructor does not parse full URLs. However, it will strip an initial leading ? off of a string, if present.
var paramsString1 = "http://example.com/search?query=%40"; var searchParams1 = new URLSearchParams(paramsString1); searchParams1.has("query"); // false searchParams1.has("http://example.com/search?query"); // true searchParams1.get("query"); // null searchParams1.get("http://example.com/search?query"); // "@" (equivalent to decodeURIComponent('%40')) var paramsString2 = "?query=value"; var searchParams2 = new URLSearchParams(paramsString2); searchParams2.has("query"); // true var url = new URL("http://example.com/search?query=%40"); var searchParams3 = new URLSearchParams(url.search); searchParams3.has("query") // true
Upvotes: 1