当我输入时
我在所有属性上都得到了未定义(undefined)。
这是我的控制台(console):
window.location
我在所有属性上都得到了未定义(undefined)。
这是我的控制台(console):
window.location
这段代码适用于Safari(6.0.1)及更早版本。
// Get location for REST TARGETS
lcnURI = window.location.protocol + "//" + window.location.hostname + ":"
+ window.location.port + "/rest/";
看起来当你在日志中查看window.location时,它会“失去上下文”,但在使用时它是正常的。
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
JSON.parse(JSON.stringify(location));
运行良好(像reload
这样的方法会丢失,但是您无论如何都不需要这些来进行检查)。 - Rob W