使用 Javascript 获取当前域名(不是路径等)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11401897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get The Current Domain Name With Javascript (Not the path, etc.)
提问by Freesn?w
I plan on buying two domain names for the same site. Depending on which domain is used I plan on providing slightly different data on the page. Is there a way for me to detect the actual domain name that the page is loading from so that I know what to change my content to?
我打算为同一个站点购买两个域名。根据使用的域,我计划在页面上提供略有不同的数据。有没有办法让我检测加载页面的实际域名,以便我知道将我的内容更改为什么?
I've looked around for stuff like this but most of it doesn't work the way I want it to.
我已经环顾四周寻找这样的东西,但其中大部分都没有按照我想要的方式工作。
For instance when using
例如当使用
document.write(document.location)
on JSFiddleit returns
在JSFiddle 上它返回
http://fiddle.jshell.net/_display/
http://fiddle.jshell.net/_display/
i.e. the actual path or whatever that is.
即实际路径或其他任何内容。
回答by Gareth
How about:
怎么样:
window.location.hostname
The location
object actually has a number of attributesreferring to different parts of the URL
回答by Sunil shakya
Try to run in script : path: http://localhost:4200/landing?query=1#2
尝试在脚本中运行:路径:http://localhost:4200/landing?query=1#2
console.log(window.location.hash)
Location have following values:
位置具有以下值:
window.location.hash: "#2"
?
window.location.host: "localhost:4200"
?
window.location.hostname: "localhost"
?
window.location.href: "http://localhost:4200/landing?query=1#2"
?
window.location.origin: "http://localhost:4200"
?
window.location.pathname: "/landing"
?
window.location.port: "4200"
?
window.location.protocol: "http:"
window.location.search: "?query=1"
回答by cprcrack
If you are not interested in the host name (for example www.beta.example.com
) but in the domain name (for example example.com
), this works for valid host names:
如果您对主机名(例如www.beta.example.com
)不感兴趣,但对域名(例如example.com
)感兴趣,这适用于有效的主机名:
function getDomainName(hostName)
{
return hostName.substring(hostName.lastIndexOf(".", hostName.lastIndexOf(".") - 1) + 1);
}
回答by adelineu
function getDomain(url, subdomain) {
subdomain = subdomain || false;
url = url.replace(/(https?:\/\/)?(www.)?/i, '');
if (!subdomain) {
url = url.split('.');
url = url.slice(url.length - 2).join('.');
}
if (url.indexOf('/') !== -1) {
return url.split('/')[0];
}
return url;
}
Examples
例子
- getDomain('http://www.example.com'); // example.com
- getDomain('www.example.com'); // example.com
- getDomain('http://blog.example.com', true); // blog.example.com
- getDomain(location.href); // ..
- getDomain(' http://www.example.com'); //example.com
- getDomain('www.example.com'); //example.com
- getDomain(' http://blog.example.com', true); // blog.example.com
- getDomain(location.href); //..
Previous version was getting full domain (including subdomain). Now it determines the right domain depending on preference. So that when a 2nd argument is provided as true it will include the subdomain, otherwise it returns only the 'main domain'
以前的版本正在获取完整域(包括子域)。现在它根据偏好确定正确的域。因此,当第二个参数提供为 true 时,它将包含子域,否则仅返回“主域”
回答by Iman Sedighi
You can get it from location object in Javascript easily:
您可以轻松地从 Javascript 中的位置对象获取它:
For example URL of this page is:
例如这个页面的 URL 是:
http://www.stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc
Then we can get the exact domain with following properties of location object:
然后我们可以使用 location 对象的以下属性获得确切的域:
location.host = "www.stackoverflow.com"
location.protocol= "http:"
you can make the full domain with:
您可以使用以下方法创建完整域:
location.protocol + "//" + location.host
Which in this example returns http://www.stackoverflow.com
在这个例子中返回 http://www.stackoverflow.com
I addition of this we can get full URL and also the path with other properties of location object:
除此之外,我们可以获得完整的 URL 以及位置对象的其他属性的路径:
location.href= "http://www.stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc"
location.pathname= "questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc"
回答by dmck
If you are only interested in the domain name and want to ignore the subdomain then you need to parse it out of host
and hostname
.
如果您只对域名感兴趣并想忽略子域,则需要将其解析为host
和hostname
。
The following code does this:
以下代码执行此操作:
var firstDot = window.location.hostname.indexOf('.');
var tld = ".net";
var isSubdomain = firstDot < window.location.hostname.indexOf(tld);
var domain;
if (isSubdomain) {
domain = window.location.hostname.substring(firstDot == -1 ? 0 : firstDot + 1);
}
else {
domain = window.location.hostname;
}
回答by Ethan
Since this question asks for domain name, nothost name, a correct answer should be
由于这个问题要求的是域名,而不是主机名,正确答案应该是
window.location.hostname.split('.').slice(-2).join('.')
This works for host names like www.example.comtoo.
这也适用于www.example.com等主机名。
回答by Dancrumb
回答by Ricardo Fran?a
If you wish a full domain origin, you can use this:
如果你想要一个完整的域来源,你可以使用这个:
document.location.origin
And if you wish to get only the domain, use can you just this:
如果您只想获得域,请使用 can you just this:
document.location.hostname
But you have other options, take a look at the properties in:
但是您还有其他选择,请查看以下属性:
document.location
回答by unixdebian11
What about this function?
这个功能怎么样?
window.location.hostname.match(/\w*\.\w*$/gi)[0]
This will match only the domain name regardless if its a subdomain or a main domain
这将只匹配域名,无论它是子域还是主域