60

I have domain name for eq.

1) http://www.abc.com/search 
2) http://go.abc.com/work

I get only domain name from the above URL

Output like

1) http://www.abc.com/
2) http://go.abc.com/

how can I do?

Abhishek B.
  • 5,112
  • 14
  • 51
  • 90

10 Answers10

91

In a browser

You can leverage the browser's URL parser using an <a> element:

var hostname = $('<a>').prop('href', url).prop('hostname');

or without jQuery:

var a = document.createElement('a');
a.href = url;
var hostname = a.hostname;

(This trick is particularly useful for resolving paths relative to the current page.)

Outside of a browser (and probably more efficiently):

Use the following function:

function get_hostname(url) {
    var m = url.match(/^http:\/\/[^/]+/);
    return m ? m[0] : null;
}

Use it like this:

get_hostname("http://example.com/path");

This will return http://example.com/ as in your example output.

Hostname of the current page

If you are only trying the get the hostname of the current page, use document.location.hostname.

Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
62

This worked for me.

http://tech-blog.maddyzone.com/javascript/get-current-url-javascript-jquery

$(location).attr('host');                        www.test.com:8082
$(location).attr('hostname');                    www.test.com
$(location).attr('port');                        8082
$(location).attr('protocol');                    http:
$(location).attr('pathname');                    index.php
$(location).attr('href');                        http://www.test.com:8082/index.php#tab2
$(location).attr('hash');                       #tab2
$(location).attr('search');                     ?foo=123
vgru
  • 49,838
  • 16
  • 120
  • 201
Adam Mendoza
  • 5,419
  • 2
  • 25
  • 31
15

Try like this.

var hostname = window.location.origin

If the URL is "http://example.com/path" then you will get  "http://example.com" as the result.

This won't work for local domains

When you have URL like "https://localhost/MyProposal/MyDir/MyTestPage.aspx" 
and your virtual directory path is "https://localhost/MyProposal/" 
In such cases, you will get "https://localhost".
sudhansu63
  • 6,025
  • 4
  • 39
  • 52
10

You can do this with plain js by using

  1. location.host , same as document.location.hostname
  2. document.domain Not recommended
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
  • 6
    `document.domain` is not the correct way of retrieving the current hostname; it's a setting used for [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). Also, `location.host` is `document.location.hostname` ;-) – Arnaud Le Blanc Jan 27 '11 at 11:24
7

You don't need jQuery for this, as simple javascript will suffice:

alert(document.domain);

See it in action:

console.log("Output;");  
console.log(location.hostname);
console.log(document.domain);
alert(window.location.hostname)

console.log("document.URL : "+document.URL);
console.log("document.location.href : "+document.location.href);
console.log("document.location.origin : "+document.location.origin);
console.log("document.location.hostname : "+document.location.hostname);
console.log("document.location.host : "+document.location.host);
console.log("document.location.pathname : "+document.location.pathname);

for more details click here window.location

just append "http://" before domain name to get appropriate result.

5

You can use a trick, by creating a <a>-element, then setting the string to the href of that <a>-element and then you have a Location object you can get the hostname from.

You could either add a method to the String prototype:

String.prototype.toLocation = function() {
    var a = document.createElement('a');
    a.href = this;
    return a;
};

and use it like this:
"http://www.abc.com/search".toLocation().hostname

or make it a function:

function toLocation(url) {
    var a = document.createElement('a');
    a.href = url;
    return a;
};

and use it like this:
toLocation("http://www.abc.com/search").hostname

both of these will output: "www.abc.com"

If you also need the protocol, you can do something like this:

var url = "http://www.abc.com/search".toLocation();
url.protocol + "//" + url.hostname

which will output: "http://www.abc.com"

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
3

While pure JavaScript is sufficient here, I still prefer the jQuery approach. After all, the ask was to get the hostname using jQuery.

var hostName = $(location).attr('hostname');      // www.example.com
Taylor Cox
  • 111
  • 1
  • 5
0

To get the url as well as the protocol used we can try the code below.

For example to get the domain as well as the protocol used (http/https).

https://google.com

You can use -

host = window.location.protocol+'//'+window.location.hostname+'/';

It'll return you the protocol as well as domain name. https://google.com/

lazycipher
  • 340
  • 3
  • 14
0
var hostname = window.location.origin

Will not work for IE. For IE support as well I would something like this:

var hostName = window.location.hostname;
var protocol = window.locatrion.protocol;
var finalUrl = protocol + '//' + hostname;
-1

try this code below it works fine with me.

example below is getting the host and redirecting to another page.

var host = $(location).attr('host');
window.location.replace("http://"+host+"/TEST_PROJECT/INDEXINGPAGE");
Hermes
  • 41
  • 3