Possible Duplicate:
How do I enumerate the properties of a javascript object?
Good day!
I want to determine all the properties of my navigator using javascript by doing this following code:
<script type="text/javascript">
for(var property in navigator){
str="navigator."+ property; //HAVING A PROBLEM HERE...
document.write(property+ " <em>"+
str+"</em><br />");
}
</script>
But the concatenation of my str variable prints as it is. What i need is the actual value of the property.
eg. navigator.appCodeName should print mozilla instead of navigator.appCodeName itself.
Thank you in advance.
1 Answer. The explanation is: The property navigator. appCodeName is the same in both Netscape and IE. The appCodeName property returns the code name of the browser.
The method console. dir() displays an interactive list of the properties of the specified JavaScript object.
Every property in JavaScript objects can be classified by three factors: Enumerable or non-enumerable; String or symbol; Own property or inherited property from the prototype chain.
You want to use navigator[property]
to access the values assigned to the properties.
for(var property in navigator){
var str = navigator[property]
document.write(property+ " <em>"+str+"</em><br />");
}
You could also benefit from dropping document.write()
, it's rarely the best way to modify the DOM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With