270

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
Tyler
  • 2,767
  • 2
  • 17
  • 7
  • possible duplicate of [Remove querystring from URL](http://stackoverflow.com/questions/2540969/remove-querystring-from-url) – Alex Angas Jan 20 '14 at 05:02

10 Answers10

404

This is possible, but you'll have to build it manually from the location object:

location.protocol + '//' + location.host + location.pathname
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
  • 17
    Note that you don't need to supply the location.protocol as all modern browsers (including IE6) will automatically inherit the current protocol. ie: `'//' + location.host + location.pathname` – JonnyReeves Feb 14 '12 at 16:07
  • 13
    @JonnyReeves If you're using it in the current document, that's true: good point. There are occasions when it might be necessary, e.g. if outputting the URL as plain text. – lonesomeday Feb 14 '12 at 16:10
  • @lonesomeday **Beautiful!** I used to hack it with ``. Since I was always in a PHP environment anyway. [$_SERVER](http://php.net/reserved.variables.server) – maxpower9000 Aug 31 '15 at 09:36
  • You don't HAVE to do it this way, as the response implies. – Oddman Feb 22 '16 at 19:39
  • 1
    @lonesomeday your response implies it's the only way, which it isn't. – Oddman Jul 09 '16 at 12:50
  • 10
    It's missing port, so this will return incorrect result for page `http://www.example.com:8080/asdf.html?foo=bar` – izogfif Aug 17 '18 at 15:00
  • 12
    You can save a few characters with `location.origin`, which I believe also addresses @izogfif's concern. – Coderer Sep 27 '18 at 09:32
  • 1
    @Coderer `location.origin` doesn't work for local links: instead of `file://` it gives you `null`. – izogfif Mar 31 '19 at 06:41
  • both this method and `location.origin` handles not special cases like `javascript:void(0)` which would not be unusual when traversing `href`s of links and buttons. – Meow Cat 2012 Dec 03 '20 at 09:58
  • @MeowCat2012 That's fair (even though `javascript:` links are not generally good practice). If they're around, a different approach would be needed. – lonesomeday Dec 03 '20 at 20:43
  • Maybe I should replace them with `#`s... And hello! SoF is really one of those rare places that 10-year-old accounts can be still active. Feels like time travelling. Sorry bothering you to look at a thread that one could already forgotten over years... – Meow Cat 2012 Dec 04 '20 at 01:56
  • @MeowCat2012 Yeah, I'm nowhere near as active as I once was, but I still want to take care of my old answers. (And FWIW, an `a` element with a `href` with only a hash will still show all the appropriate `host`, `pathname` etc. properties.) – lonesomeday Dec 04 '20 at 23:50
252

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.

Oddman
  • 3,715
  • 1
  • 16
  • 13
38

I'm LATE to the party, but I had to solve this recently, figured I'd share the wealth.

const url = window.location.origin + window.location.pathname
//http://example.com/somedir/somefile/

window.location.origin will give you the base url, in our test case: http://example.com

window.location.pathname will give you the route path (after the base url), in our test case /somedir/somefile

SOLUTION 2

You can simply do the following to get rid of the query parameters.

const url = window.location.href.split('?')[0]

Robo Rick
  • 721
  • 6
  • 7
15

Use indexOf

var url = "http://mysite.com/somedir/somefile/?aa";

if (url.indexOf("?")>-1){
url = url.substr(0,url.indexOf("?"));
}
Niklas
  • 29,752
  • 5
  • 50
  • 71
12

Use the URL() constructor, then extract and concatenate the origin and pathname. This will automatically strip the search (aka query) parameters from the url, leaving the scheme, domain, port and pathname only.

const url = new URL('http://example.com/somedir/?foo=bar');
console.log(url.origin + url.pathname);

As a note, this type of transformation is usually referred to as normalization, specifically in this case URI Normalization. There may already exist libraries that accomplish this more robustly with more options in your environment.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33
11

You can concat origin and pathname, if theres present a port such as example.com:80, that will be included as well.

location.origin + location.pathname
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • This solution doesn't work with Microsoft Internet Explorer <= 10: https://developer.mozilla.org/en-US/docs/Web/API/Location/origin – gouessej Aug 08 '20 at 09:48
10

Just one more alternative using URL

var theUrl = new URL(window.location.href);
theUrl.search = ""; //Remove any params
theUrl //as URL object
theUrl.href //as a string
digitalWestie
  • 2,647
  • 6
  • 28
  • 45
5

You can use a regular expression: window.location.href.match(/^[^\#\?]+/)[0]

Mic
  • 24,812
  • 9
  • 57
  • 70
4
var url = "tp://mysite.com/somedir/somefile/?foo=bar&loo=goo"    

url.substring(0,url.indexOf("?"));
Headshota
  • 21,021
  • 11
  • 61
  • 82
0

If you look at the documentation you can take just the properties you're interested in from the window object i.e.

protocol + '//' + hostname + pathname
planetjones
  • 12,469
  • 5
  • 50
  • 51
  • You lose the port number with your suggestion, lonesomeday's solution is better in my humble opinion. – gouessej Aug 08 '20 at 09:47