pstein
August 13, 2024, 6:29am
1
Occasionally webpages show an URL with an appended “?” and a tracking code like
http://www.foobar.com/some/path/before.html?after-is-tracking=12345
Assume I assign this URL to a variable:
How can I remove (with Javascript or even better with jQuery) everything after “?” including the “?” itself?
rpkamp
August 13, 2024, 7:13am
2
var url = new URL('http://www.foobar.com/some/path/before.html?after-is-tracking=12345');
url.hash = '';
url.search = '';
console.log(url.toString());
This strips both the query string as well as any fragment hash from the URL.
(answer found here )
1 Like
…
thestring.split("?")[0]
(There will only ever be one ? in a URL because it’s a special character. Which means basic string manipulation works without casting to objects and whatnot.)
2 Likes