How can I get the domain name with jquery ??
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);
//If url is something.domain.com this returns -> domain.com
function getDomain() {
return window.location.hostname.replace(/([a-z]+.)/,"");
}
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 )
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.
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
.
//If url is something.domain.com this returns -> domain.com
function getDomain() {
return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
}