为什么在Safari 6和Mobile Safari(iOS 6)中的所有属性上都未定义window.location

时间:2012-06-25 22:36:02

标签: safari mobile-safari

当我输入

window.location

我在所有属性上都未定义。

这是我的控制台:

enter image description here

2 个答案:

答案 0 :(得分:5)

此代码适用于Safari(6.0.1)及更早版本

    // Get location for REST TARGETS

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

当你在日志中查看时,window.location似乎“脱离了上下文”。但到位使用它很好。

答案 1 :(得分:4)

我可以确认一下(版本6.0(8536.25))。据我所知,似乎是一个Safari漏洞。

在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