我正在寫一個書籤,其中顯示當前頁面的location.hostname
。某些網頁的location.hostname不準確
在大多數網站上這工作正常,即https://stackoverflow.com/questions/ask它返回http://stackoverflow.com
。
但在某些網站上它不起作用。例如,在http://techslides.com/over-2000-d3-js-examples-and-demos上它返回googleads.g.doubleclick.net
。
我已經想通了,我可以用location.ancestorOrigins[0]
使用這種「原始」主機名http://techslides.com
,我已經寫了下面的嘗試得到在任何網站上的精確主機名:
function getHostname() {
var previous_hostnames = window.ancestorOrigins;
if (previous_hostnames) {
return previous_hostnames[0];
} else {
return window.hostname;
}
}
我在想:
- 這是怎麼發生的?
- 我的方法是否足夠?
可以假設我只是在Chrome上運行,如果它使事情變得更容易。
Chrome瀏覽器版本:
Version 52.0.2743.116 (64-bit)
Platform 8350.68.0 (Official Build) stable-channel orco
Firmware Google_Orco.5216.362.7
響應意見
既不location.host
或location.origin
工作
location.host
"googleads.g.doubleclick.net"
location.origin
"https://googleads.g.doubleclick.net"
什麼'location.host'或'location.origin'? – Scott
@Scott不起作用,請參閱更新的問題與證明 –
嗯,我試圖在2000年的D3例子網站,他們都返回正確的結果。即使'.hostname'也適用於我。沒有運行Adblock或類似的東西。希望別人能夠衡量這一點,對不起! – Scott