Sometimes, we want to remove query string from URL with JavaScript.
In this article, we’ll look at how to remove query string from URL with JavaScript.
How to remove query string from URL with JavaScript?
To remove query string from URL with JavaScript, we can use the URL
constructor.
For instance, we write
const getPathname = (path) => {
return new URL(`http://_${path}`).pathname;
};
const pathname = getPathname("/foo/bar?baz=5");
to create a URL
object with the http://_${path}
to get the URL
object.
Then we get the part of the URL before the query string with the pathname
property.
Conclusion
To remove query string from URL with JavaScript, we can use the URL
constructor.