Reputation: 45
I am trying to get value from the URL using js. Everything works fine but when the URL containing any space then it is capturing only the value before that space char.
let searchParams = new URLSearchParams(window.location.search);
// An example url is https://localhost:3000/all_posts?city=%27Auburn%27&catagory=%27Bus%20&%20Trucks%27
let catagory = searchParams.get("catagory");
console.log(catagory);
It returns 'Bus only
How can I get the full value?
Upvotes: 0
Views: 452
Reputation: 542
The problem is not with the space, the problem is with the &
character.
&
character in the URL
has the separator role for the query string
so you need to change &
with %26
.
Upvotes: 3