293

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
1

11 Answers 11

426

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

location.origin + location.pathname
Sign up to request clarification or add additional context in comments.

13 Comments

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 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.
It's missing port, so this will return incorrect result for page http://www.example.com:8080/asdf.html?foo=bar
You can save a few characters with location.origin, which I believe also addresses @izogfif's concern.
@izogfif (really old post I know) You said this solution is missing port, but I don't believe it is. location.hostname returns the host name without the port, location.host should return the complete host value (port included). Just so others are aware of the difference between the two.
|
276

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.

8 Comments

in case you need to remove the anchors window.location.href.split(/[?#]/)[0];
indeed every answer is rather convoluted
@mehmet all ears if you have a simpler approach :)
@Oddman, I absolutely don't remember what I was thinking then, but I guess that might be an affirmative statement :))
@Vishal--JAVA--CQ "better" is subjective, but it's simpler.
|
44

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]

1 Comment

Solution 2 is not complete and will include the hashes (#) if come before the QSPs.
17

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.

Comments

15

Use indexOf

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

if (url.indexOf("?")>-1){
url = url.substr(0,url.indexOf("?"));
}

1 Comment

It doesn't remove #.
12

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

1 Comment

This solution doesn't work with Microsoft Internet Explorer <= 10: developer.mozilla.org/en-US/docs/Web/API/Location/origin
12

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

Comments

5

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

Comments

4
var url = "tp://mysite.com/somedir/somefile/?foo=bar&loo=goo"    

url.substring(0,url.indexOf("?"));

1 Comment

This presumes, that parameter is defined. Much better is url.split('?')[0]
1

You can do this

window.location.origin + window.location.pathname

Comments

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

1 Comment

You lose the port number with your suggestion, lonesomeday's solution is better in my humble opinion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.