2012-07-09 68 views
117

我打算为同一个网站购买两个域名。根据使用的域名,我计划在页面上提供稍微不同的数据。有没有办法让我检测页面加载的实际域名,以便知道如何更改我的内容?使用Javascript获取当前域名(不是路径等)

我环顾四周寻找这样的东西,但大部分并不按照我希望的方式工作。

例如使用

document.write(document.location) 

JSFiddle它返回

http://fiddle.jshell.net/_display/

即实际路径或无论是。

+1

我不确定我是否完全明白你想要做什么,但你应该看看[MDN](https://developer.mozilla.org/en/DOM/window.location)关于这个 – MilkyWayJoe 2012-07-09 19:39:14

+0

有点topi c,但你也可以考虑拥有子域名而不是两个独立的域名。类似于'premium.random.com'和'free.random.com' – 2017-01-10 08:29:55

回答

257

如何:

window.location.hostname 

location对象实际上有一个number of attributes指的是URL的不同部分

+2

http://jsfiddle.net/5U366/ – FreeSnow 2012-07-09 19:41:02

+28

和'window.location.hostname',如果你不想得到'port'(像'http:// localhost:3000 /','window.location.host ='localhost:3000''和'window.location。主机名=“localhost'' – Guilherme 2015-02-16 14:32:21

+2

答案是错误的,因为口不属于域。 'window.location.host'有时会给端口返回。 – Andrej 2016-03-14 14:54:23

8

使用

document.write(document.location.hostname)​ 

window.location有一堆属性。请参阅here以获取它们的列表。

+1

http://jsfiddle.net/5U366/1/ – FreeSnow 2012-07-09 19:41:18

7

如果您只对域名感兴趣并且想忽略子域名,那么您需要将其解析为hosthostname

下面的代码做到这一点:

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

http://jsfiddle.net/5U366/4/

+0

为什么它返回jShell代替的jsfiddle? – FreeSnow 2012-07-09 19:55:51

+2

@XanderLamkins:它的iframe:http://jsfiddle.net/5U366/ <-> http://fiddle.jshell.net/5U366/show/ – Saxoier 2012-07-09 19:57:31

21

如果你不感兴趣的主机名(例如www.beta.example.com),但在域名(例如example.com),这适用于有效主机名:

function getDomainName(hostName) 
{ 
    return hostName.substring(hostName.lastIndexOf(".", hostName.lastIndexOf(".") - 1) + 1); 
} 
+7

不正确的,这将打破几乎所有的国际顶级域名。 – tbranyen 2014-09-19 02:30:18

+3

@tbranyen你能解释一下你指的是?我能想到的唯一情况是一样'amazon.co.uk'子域。但绝对不是“几乎所有的国际顶级域名”。事实上,它的工作原理与[在维基百科找到的国家代码]的100%(http://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Country_code_top-level_domains) – cprcrack 2014-09-21 00:53:44

+1

cprcrack由于某种原因,我想更多的国际(美国)tlds包含多个点。感谢您的纠正! – tbranyen 2014-09-21 15:54:58

11
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; 
} 

个实例

以前的版本正在获取完整域(包括子域)。现在它根据偏好确定正确的域。所以,当第二个参数为真只要将包含子,否则仅返回“主域”

+1

这应该是答案,它甚至在'localhost/test.php'工作,并且正确答案'localhost'。 – Mohammad 2016-07-12 14:07:19

+0

这也是最好的答案,因为它适用于你传入的任何URL,而不仅仅是当前window.location – sohtimsso1970 2017-03-10 15:17:32

3

如果你想全域原点,您可以使用此:

document.location.origin 

如果你希望得到的只是域名,可以使用你刚才这样的:

document.location.hostname 

但你还有其他的选择,看看在性能:

document.location 
5

因为这个问题问的域名,主机名,正确的答案应该是

window.location.hostname.split('.').slice(-2).join('.') 

这适用于像www.example.com太主机名。

+0

这个工作,但不能与domain.co.uk,domain.com.br等域名一起工作..我怎么能使其与任何域的工作? – Sameh 2017-06-18 05:12:35

4

您可以在Javascript中的位置对象得到它容易:

对于这个页面的例子网址是:

http://www.stackoverflow.com/questions/11401897/get-the-current-domain-name-with-javascript-not-the-path-etc 

然后我们就可以得到确切的域与以下位置对象的属性:

location.host = "www.stackoverflow.com" 
location.protocol= "http:" 

可以使全域有:

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

在这个例子中返回http://www.stackoverflow.com

我加入这一点,我们可以得到完整的网址,并与定位对象的其他属性的路径:

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" 
1

我想,这应该是那样简单这样的:

url.split("/")[2]

1

如果你想在JavaScript中获取域名,只需要使用下面的代码:

var domain_name = document.location.hostname; 
alert(domain_name); 

如果你需要的网页的URL路径,以便您可以访问网页的URL路径用这个例子:

var url = document.URL; 
alert(url); 

,或者你可以see this guide

0

我的情况的最佳匹配是window.location的.origin