2013-05-29 2 views
-1

Выполнения follwing код в браузере:Javascript свойства местоположение не определено

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 
+1

'местоположение [propt]', 'screen' не существует. – elclanrs

ответ

2

Как уже упоминалось here, вам нужно обернуть тело 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"); 
    } 
} 
1

Эээ ... Вы циклически location, но доступ к свойствам на screen. Этот код скопирован?

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

, кажется, работает

Смежные вопросы