How to get the URL without any parameters in JavaScript?
If I use:
alert(window.location.href);
I get everything including query strings. Is there a way to just get the main url part, for example:
http://mysite.com/somedir/somefile/
instead of
http://mysite.com/somedir/somefile/?foo=bar&loo=goo
Solution 1:
This is possible, but you'll have to build it manually from the location
object:
location.protocol + '//' + location.host + location.pathname
Solution 2:
Every answer is rather convoluted. Here:
var url = window.location.href.split('?')[0];
Even if a ? isn't present, it'll still return the first argument, which will be your full URL, minus query string.
It's also protocol-agnostic, meaning you could even use it for things like ftp, itunes.etc.