41

I'm looking for a neat way of getting the URL of the current document in Javascript.

  • The URL should be clean of parameters (?parameter1=bla&parameter2=bla)
  • The URL should be clean of hash tags (#jumppoint)
  • http/https should be removed/consolidated into http

I know i can get the current URL with location.href and then use some regular expressions to clean it up but maybe there is a nicer/cleaner solution for getting rid of the junk?

3
  • I'm tempted to downvote just for calling important parts of the url "junk"! Commented Mar 1, 2012 at 9:39
  • @Douglas Importance is subjective. It's entirely plausible that in the context of this function only the host is the relevant part. Commented Mar 1, 2012 at 9:40
  • ptorocol can be ftp: as well, do you need to consider it? Commented Mar 1, 2012 at 9:47

8 Answers 8

62

There are many other parameters than the href in window.location. See full reference here: https://developer.mozilla.org/en/DOM/window.location

What you are looking for as a starter might be the window.location.hostname:

"the host name (without the port number or square brackets)."

From the example URL http://[www.example.com]:80/search?q=devmo#test the hostname will be www.example.com.

If you also want to include the path and force a http:// protocol, try:

'http://' + window.location.hostname + window.location.pathname;

As a side note, a nifty trick to get the same parameters from another URL than the window.location is to create an empty anchor:

var a = document.createElement('a');
a.href = 'http://www.example.com:80/search?q=devmo#test';

console.log('http://' + a.hostname + a.pathname);
Sign up to request clarification or add additional context in comments.

3 Comments

I think they want the pathname too.
@jfriend00 yea, the docs should be enough to figure things out, but I added that too for clarity.
'http://' + window.location.hostname + window.location.pathname; you forgot the port
35

None of the given answers address the fact that the protocol can be http or https as in the OPs title. To accommodate this I suggest:

document.location.protocol +"//"+ document.location.hostname + document.location.pathname

1 Comment

If you have a port number in your URL, this solution causes the port number to be lost, ie localhost:49346 becomes localhost.
5

The Location object got what you need

window.location.hostname + window.location.pathname

Comments

2

You have document.location object, so:

var oLoc = document.location,
    sUrl = oLoc.protocol + oLoc.hostname;
    // or "http://" + oLoc.hostname

Comments

2

You can use these replacement functions to remove the hash and search arguments and normalize https to http:

url = url.replace(/#[^#]*$/, "").replace(/\?[^\?]*$/, "").replace(/^https:/, "http:");

Or, if all you really want is the domain and path, you can just use this:

window.location.hostname + window.location.pathname

Comments

2

Please try this snippet:

if (!window.location.origin){
  // For IE
  window.location.origin = window.location.protocol + "//" + (window.location.port ? ':' + window.location.port : '');      
}

url = window.location.origin + window.location.pathname;
document.write('Origin url: ' + url);

3 Comments

This was already answered long ago. And this answer is wrong regarding the question as it will still provide https:// as the origin protocol if the page is https - but it should strip the protocol/readd it as http.
Isn't there the host name missing in the for-IE code? Like window.location.origin = window.location.protocol + "//" + (window.location.port ? ':' + window.location.port : '') + '/' + window.location.hostname;
yes, but not in that order : window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '');
0

This page indicates that you could probably use window.location.host to get the part you're actually interested in. I haven't tested it, though.

Comments

0

Try:


window.location.hostname;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.