我正在写一个书签,其中显示当前页面的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