145

How can I get the domain name with jquery ??

Sampson
  • 251,934
  • 70
  • 517
  • 549
AlexC
  • 9,529
  • 17
  • 61
  • 96
  • I think my this answer is not found in web : If I want to get .com from www.example.com Try this code, and you will get "com". `var part = location.hostname.split('.'); var subdomains = part.shift(); var TLDextension = part.join('.');` – Shurvir Mori Feb 13 '21 at 07:38

11 Answers11

342

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 further domain-related values, check out the properties of window.location online. You may find that location.host is a better option, as its content could differ from document.domain. For instance, the url http://192.168.1.80:8080 will have only the ipaddress in document.domain, but both the ipaddress and port number in location.host.

Maximillian Laumeister
  • 18,162
  • 7
  • 50
  • 70
Sampson
  • 251,934
  • 70
  • 517
  • 549
  • 7
    @rob.alarcon It's a [W3C recommendation](http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-2250147) and has very broad support among browsers, old and new. The only issue I know of is that some browsers ([such as Firefox](https://developer.mozilla.org/en-US/docs/DOM/document.domain#Notes)) will let you write to this property (readonly per the spec) as long as the new value is a subdomain of the original domain. – Sampson Oct 18 '12 at 19:53
  • 4
    I would actually recommend using `window.location.host` (as bibby posted). I just fixed a bug where `document.domain` was being set to the root domain instead of the subdomain. – Derek Morrison Mar 15 '13 at 20:03
  • In IE11 `document.location.origin` returns `undefined`. – TimothyBuktu May 19 '16 at 09:11
115

EDIT:

If you don't need to support IE10, you can simply use: document.location.origin

Original answer, if you need legacy support

You can get all this and more by inspecting the location object:

location = {
  host: "stackoverflow.com",
  hostname: "stackoverflow.com",
  href: "http://stackoverflow.com/questions/2300771/jquery-domain-get-url",
  pathname: "/questions/2300771/jquery-domain-get-url",
  port: "",
  protocol: "http:"
}

so:

location.host 

would be the domain, in this case stackoverflow.com. For the complete first part of the url, you can use:

location.protocol + "//" + location.host

which in this case would be http://stackoverflow.com

No jQuery required.

superluminary
  • 38,944
  • 21
  • 142
  • 143
  • related http://stackoverflow.com/questions/6549117/whats-the-difference-between-window-location-host-and-window-location-hostname – Adrien Be Nov 19 '13 at 13:14
  • @ For the complete url, why not use `document.location.origin` which will result to [https://stackoverflow.com](https://stackoverflow.com). This method includes the appropriate protocol – Nathileo Apr 14 '20 at 15:34
33

Similar to the answer before there there is

location.host

The location global has more fun facts about the current url as well. ( protocol, host, port, pathname, search, hash )

bibby
  • 837
  • 1
  • 8
  • 13
20

If you need from string, like me, use this function - it really works.

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

But note, if there is a subdomain (e.g. www.) in the URL it will get returned with the hostname. Conversely, if there is no subdomain the hostname will not have one either.

Hannele
  • 7,820
  • 5
  • 46
  • 64
Vova Popov
  • 1,027
  • 11
  • 11
  • Could @Vova Popov (or someone) please clarify the last sentence? "www. ar no www."?!? – Randall Cook Dec 04 '12 at 01:03
  • 2
    He's saying that a.hostname will return the www. that's part of your domain. So in other words, this would be false: `getHost('http://www.google.com') == 'google.com'` whereas this would be true: `getHost('http://google.com') == 'google.com'` – Milimetric Dec 27 '12 at 22:39
9

You can use below codes for get different parameters of Current URL

alert("document.URL : "+document.URL);
alert("document.location.href : "+document.location.href);
alert("document.location.origin : "+document.location.origin);
alert("document.location.hostname : "+document.location.hostname);
alert("document.location.host : "+document.location.host);
alert("document.location.pathname : "+document.location.pathname);
Nimesh07
  • 352
  • 3
  • 5
  • is there also a way to get the host where the js file is hosted instead of the document? atm. i'm manipulating the js file server side to get this "connection" string which works but looks inefficiant since i would prefer a static js file. – yellowsir Jul 03 '15 at 10:12
6

jQuery is not needed, use simple javascript:

document.domain
Adam
  • 191
  • 2
  • 4
  • maybe it is not need it but saying like that you are not answer the question – Emiliano Oct 19 '18 at 22:18
  • Yeah, i'm not a robot that answers the question literally. I answered the question pragmatically knowing what the end goal was and that you don't need jQuery to attain the goal. – Adam Dec 05 '18 at 15:58
  • I understand but that kind of answer is not complete, if you want you can do a better answer – Emiliano Dec 05 '18 at 18:59
3

document.baseURI gives you the domain + port. It's used if an image tag uses a relative instead of an absolute path. Probably already solved, but it might be useful for other guys.

jsbeckr
  • 1,172
  • 1
  • 9
  • 24
2
var part = location.hostname.split('.');
var subdomains = part.shift();
var upperleveldomains = part.join('.');

second-level-domain, you might use

var sleveldomain = parts.slice(-2).join('.');
1

check this

alert(window.location.hostname);

this will return host name as www.domain.com

code.rider
  • 1,634
  • 17
  • 20
-2
//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-z]+.)/,"");
}
Pang
  • 8,605
  • 144
  • 77
  • 113
Gubatron
  • 5,581
  • 5
  • 31
  • 35
-4
//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
}
Gubatron
  • 5,581
  • 5
  • 31
  • 35
  • 3
    but if the url already dont have the prefix, it will remove the domain name.. Example: "stackoveflow.com" will become "com" – Filipiz Aug 23 '12 at 18:06
  • `Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.` - Jamie Zawinski – msangel Jun 23 '13 at 06:49