How to extract the hostname portion of a URL in JavaScript

suppose that you have a page with this address: use the following in page code to achieve those results:

  • : you'll get or
  • window.location.hostname : you'll get
  • window.location.protocol : you'll get http:
  • window.location.port : you'll get 8080 or 80
  • window.location.pathname : you'll get /virtualPath
  • window.location.origin : you'll get *****

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 if the port is anything but 80, and if the port is 80.

Special thanks to @torazaburo for mentioning that to me.

You could concatenate the location protocol and the host:

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

For a url, let say '', it will return ''