-1

当我在浏览器中执行以下代码时:

for (propt in location) {
   document.write("location property " + propt + " is currently: ");
   document.write(screen[propt] + "<br />\n");
}

所有属性都是未定义的。为什么会这样?

这是输出:

location property assign is currently: undefined
location property replace is currently: undefined
location property reload is currently: undefined
location property ancestorOrigins is currently: undefined
location property origin is currently: undefined
location property hash is currently: undefined
location property search is currently: undefined
location property pathname is currently: undefined
location property port is currently: undefined
location property hostname is currently: undefined
location property host is currently: undefined
location property protocol is currently: undefined
location property href is currently: undefined
4

3 回答 3

2

如此处所述,您需要for in用条件语句包装循环体。这是因为位置属性包括函数,这些函数通过:分配、替换、重新加载等。

此外,screen[propt]当您需要使用location[propt].

下面是正确的代码:

for (propt in location) {
    if (location.hasOwnProperty(propt) && typeof location[propt] !== 'function'){
        document.write("location property " + propt + " is currently: ");
        document.write(location[propt] + "<br />\n");
    }
}
于 2013-05-29T01:52:10.717 回答
1

呃...您正在循环location访问 ,但访问 上的属性screen。这段代码是你复制粘贴的吗?

document.write(location[propt] +"<br />\n");
于 2013-05-29T01:54:09.163 回答
0
for (propt in location) {
   document.write("location property " + propt + " is currently: ");
   document.write(propt + "<br />\n");
}

似乎工作

于 2013-05-29T01:54:24.647 回答