2013-05-29 4 views
-1

Lorsque j'exécute le code follwing dans mon navigateur:propriétés emplacement Javascript non défini

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

Toutes les propriétés ne sont pas définies. Pourquoi cela arrive-t-il?

est ici la sortie:

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

'lieu [propt]', 'screen' n'existe pas. – elclanrs

Répondre

2

Comme mentionné here, vous devez envelopper votre corps de la boucle for in avec une instruction conditionnelle. Cela est dû au fait que les propriétés d'emplacement incluent des fonctions qui s'affichent: assigner, remplacer, recharger, etc.

De plus, vous utilisez screen[propt] lorsque vous devez utiliser location[propt].

est en dessous du code correct:

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

... Erm Vous Boucler dans location, mais accéder aux propriétés sur screen. Ce code est-il copié-collé?

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

semble fonctionner