Take the 2-minute tour ×

Is there a really easy way to extract from


the host part



There's gotta be a JavaScript function that does this reliably, but I can't find it.

share|improve this question
add comment

8 Answers

up vote 86 down vote accepted

suppose that you have a page with this address: http://sub.domain.com/page.htm. use the following in page code to achive those results:

  • window.location.host : you'll get sub.domain.com:8080 or sub.domain.com:80
  • window.location.hostname : you'll get sub.domain.com
  • window.location.protocol : you'll get http:
  • window.location.port : you'll get 8080 or 80
  • window.location.origin : you'll get http://sub.domain.com *

Update: about the .origin

* As the ref states, browser compatibility for window.location.origin is not clear. I've checked it in chrome and it returned http://sub.domain.com:port.

Special thanks to @torazaburo for mentioning that to me.

share|improve this answer
Incorrect: origin includes port. –  torazaburo Sep 17 '13 at 13:02
@torazaburo - In chrome, you're right. In the other, I don't know... Thanks –  AminSaghi Sep 17 '13 at 14:01
add comment

You could concatenate the location protocol and the host:

var root = location.protocol + '//' + location.host;

For a url, let say 'http://stackoverflow.com/questions', it will return 'http://stackoverflow.com'

share|improve this answer
it seems like you should use "hostname" rather than "host" to achieve the above results. source: stackoverflow.com/questions/6725890/… –  user417669 Nov 10 '13 at 23:10
You can use location.origin with the same result. –  Klawztro Dec 14 '13 at 7:05
add comment

Use document.location object and its host or hostname properties.

alert(document.location.hostname); // alerts "stackoverflow.com"
share|improve this answer
add comment

There are two ways. The first is a variant of another answer here, but this one accounts for non-default ports:

function getRootUrl() {
  var defaultPorts = {"http:":80,"https:":443};

  return window.location.protocol + "//" + window.location.hostname
   + (((window.location.port)
    && (window.location.port != defaultPorts[window.location.protocol]))
    ? (":"+window.location.port) : "");

But I prefer this simpler method (which works with any URI string):

function getRootUrl(url) {
  return url.toString().replace(/^(.*\/\/[^\/?#]*).*$/,"$1");
share|improve this answer
Thank you! I like the 2nd method better too! especially when on server side javascript, no way to get window.location :) –  trillions Jul 9 '13 at 20:43
add comment




share|improve this answer
add comment

There is another hack I use and never saw in any StackOverflow response : using "src" attribute of an image will yield the complete base path of your site. For instance :

var dummy = new Image;
dummy.src = '$';                  // using '' will fail on some browsers
var root = dummy.src.slice(0,-1); // remove trailing '$'

On an URL like http://domain.com/somesite/index.html, root will be set to http://domain.com/somesite/. This also works for localhost or any valid base URL.

Note that this will cause a failed HTTP request on the $ dummy image. You can use an existing image instead to avoid this, with only slight code changes.

Another variant uses a dummy link, with no side effect on HTTP requests :

var dummy = document.createElement ('a');
dummy.href = '';
var root = dummy.href;

I did not test it on every browser, though.

share|improve this answer
add comment

check this


this will return host name as www.domain.com



will return domain name with port like www.example.com:80

for complete reference watch this



share|improve this answer
add comment



and for: "http://aaa.bbb.ccc.ddd.com/sadf.aspx?blah"

you will get: http://aaa.bbb.ccc.ddd.com/

share|improve this answer
add comment

Your Answer


