I try to get a URL parameter nr, but I get always false.
var url = window.location.href;
let params = new URLSearchParams(url.search);
let nr = params.has('nr')
What is the error?
Use
var url = window.location;
let params = new URLSearchParams(url.search);
let nr = params.has('nr')
because window.location is a Location object with a .search property
whereas window.location.href is a string, without a .search property
therefore your url.search is undefined
I can demonstrate with URL which is similar to Location in this respect
let loc = new URL('http://example.com/?nr=1');
// loc is a placeholder for your window.location
let url = loc.href;
// here, url.search would be window.location.href.search
let params = new URLSearchParams(url.search);
let nr = params.has('nr')
console.log(nr);
url = loc;
// here, url.search would be window.location.search
params = new URLSearchParams(url.search);
nr = params.has('nr')
console.log(nr);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With