Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different JavaScript object literal behaviour in FireFox & Google Chrome? [duplicate]

I have narrowed down my error to following set of codes producing different behaviour in google Chrome:

Sample Code: http://jnvxxx.appspot.com/rpc/static_server?key_=testjs.html

Firefox Output:Hi 1 [object Object]192 Hi 2

Chrome Output: Hi 1 [object Object]undefined Hi 2

Any idea hot to get attributes working in google chrome as well.

Thanks.

like image 922
Ram Shanker Avatar asked Feb 27 '26 10:02

Ram Shanker


1 Answers

You are accessing the window.status property, which is used to control the text in the status bar. See: http://www.w3schools.com/jsref/prop_win_status.asp.

Apparently, this functionality has to be turned on first in all major browsers, so apparently different browsers do different things when it's turned off. Chrome changes the value of the status property to a string, so it becomes the cryptic-but-familiar string "[object Object]", which has no entry_count property. Firefox leaves the object intact in the status property.

Bottom line: window.status is already being used for something else; use a different name for your variable.

EDIT:

As mentioned below, an even better way do do all this would be to encapsulate it in function scope, as long as you're not going to use it in other places anyway:

(function() {
   var myStatus = {...};
   // Do something with myStatus, preferably not document.write ;)
}());

var a = typeof myStatus; // a === 'undefined'.

This way, the variable will only be visible within the function scope.

like image 141
Spiny Norman Avatar answered Mar 01 '26 01:03

Spiny Norman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!