开发者

jquery, domain, get URL

开发者 https://www.devze.com 2022-12-20 14:31 出处:网络
How can I get the domain name wi开发者_C百科th jquery ?? You don\'t need jQuery for this, as simple javascript will suffice:

How can I get the domain name wi开发者_C百科th jquery ??


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.


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.


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 )


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.


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);


jQuery is not needed, use simple javascript:

document.domain


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.


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('.');


check this

alert(window.location.hostname);

this will return host name as www.domain.com


//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-z]+.)/,"");
}


//If url is something.domain.com this returns -> domain.com
function getDomain() {
  return window.location.hostname.replace(/([a-zA-Z0-9]+.)/,"");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消