簡體   English   中英

某些網頁的location.hostname不正確

[英]location.hostname of some web pages is not accurate

我正在寫一個小書簽,它讀取當前頁面的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;
  }
}

我很好奇:

  1. 為什么會這樣呢?
  2. 我的方法合適嗎?

如果可以簡化操作,可以假設我只是在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.hostlocation.origin不起作用

location.host
"googleads.g.doubleclick.net"
location.origin
"https://googleads.g.doubleclick.net"

看來該錯誤是由我的一個Chrome擴展程序引起的(不確定是哪個,以后會嘗試確定)。

禁用所有擴展名可以恢復正確的location.hostname

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM