2012-07-09 35 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