简体   繁体   English

jquery,域,获取 URL

[英]jquery, domain, get URL

如何使用 jquery 获取域名?

You don't need jQuery for this, as simple javascript will suffice:为此,您不需要 jQuery,因为简单的 javascript 就足够了:

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.有关更多域相关值,请在线查看window.location的属性。 You may find that location.host is a better option, as its content could differ from document.domain .您可能会发现location.host是更好的选择,因为它的内容可能与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 .例如,url http://192.168.1.80:8080将只有document.domain的 ipaddress ,但location.host的 ipaddress 和端口号。

EDIT:编辑:

If you don't need to support IE10, you can simply use: document.location.origin如果你不需要支持IE10,你可以简单地使用: 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.将是域,在本例中为 stackoverflow.com。 For the complete first part of the url, you can use:对于 url 的完整第一部分,您可以使用:

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

which in this case would be http://stackoverflow.com在这种情况下将是http://stackoverflow.com

No jQuery required.不需要jQuery。

Similar to the answer before there there is类似于之前的答案有

location.host

The location global has more fun facts about the current url as well. location global 也有更多关于当前 url 的有趣事实。 ( 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 (eg www.) in the URL it will get returned with the hostname.但请注意,如果 URL 中有子域(例如 www.),它将与主机名一起返回。 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您可以使用以下代码获取当前 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,使用简单的 javascript:

document.domain

document.baseURI gives you the domain + port. document.baseURI为您提供域 + 端口。 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这将返回主机名作为 www.domain.com

If you want to know the "registered domain", check this out: http://lbolla.wordpress.com/2011/04/05/get-registered-domain-in-python-and-javascript/ 如果您想知道“注册域名”,请查看: http//lbolla.wordpress.com/2011/04/05/get-registered-domain-in-python-and-javascript/

I've implemented reg-dom-libs (http://www.dkim-reputation.org/regdom-libs/) in Javascript and Python. 我在Javascript和Python中实现了reg-dom-libs(http://www.dkim-reputation.org/regdom-libs/)。

//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]+.)/,"");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM