为什么在Safari 6和移动版Safari(iOS 6)中,所有属性上的window.location都是未定义的?

9
当我输入时
window.location

我在所有属性上都得到了未定义(undefined)。
这是我的控制台(console): enter image description here

记录 JSON.parse(JSON.stringify(location)); 运行良好(像 reload 这样的方法会丢失,但是您无论如何都不需要这些来进行检查)。 - Rob W
2个回答

5

这段代码适用于Safari(6.0.1)及更早版本。

    // Get location for REST TARGETS

    lcnURI = window.location.protocol + "//" + window.location.hostname + ":"
            + window.location.port + "/rest/";

看起来当你在日志中查看window.location时,它会“失去上下文”,但在使用时它是正常的。


问题是我期望得到位置对象的所有属性,而不仅仅是URI。 - Chris Abrams

4
我可以确认这个问题(版本6.0(8536.25))。据我所知,这似乎是Safari的一个bug。
在jsbin(http://jsbin.com/enugoj/1/)上进行了测试:
console.log(window.location);
console.log(window.location.pathname);​

Safari控制台中的结果:

Location
  ancestorOrigins: undefined
  hash: undefined
  host: undefined
  hostname: undefined
  href: undefined
  origin: undefined
  pathname: undefined
  port: undefined
  protocol: undefined
  search: undefined
  __proto__: LocationPrototype

/enugoj/1

在Chrome(版本21.0.1180.89)中的结果:

Location
  ancestorOrigins: DOMStringList
  assign: function () { [native code] }
  hash: ""
  host: "jsbin.com"
  hostname: "jsbin.com"
  href: "http://jsbin.com/enugoj/1"
  origin: "http://jsbin.com"
  pathname: "/enugoj/1"
  port: ""
  protocol: "http:"
  reload: function () { [native code] }
  replace: function () { [native code] }
  search: ""
  toString: function toString() { [native code] }
  valueOf: function valueOf() { [native code] }
  __proto__: Location
  1:14

/enugoj/1 

1
是的,这正是我得到的...所有的都是未定义的 :( - Chris Abrams

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接