Bogdan Burym
Bogdan Burym

Reputation: 5512

Unable to read object properties in IE8

I have the following code, and it does not work in IE8 (works perfect in Firefox):

response = eval('({"success":true,"statuses":[{"title":"Unknown","code":"UNKNOWN","cssClass":"sys_unknown","id":1}]})');

for (var i in response.statuses) {
    status = response.statuses[i];
     if (status.id != undefined) {
        alert('ID = ' + status.id);
     }
}

Can someone tell what's wrong? Thanks.

Upvotes: 0

Views: 839

Answers (3)

Rogier Pennink
Rogier Pennink

Reputation: 418

First of all, try to always declare your variables with the var keyword. If you don't, they will become properties of the window object, effectively adding them to the global scope.

The reason why it doesn't work is because of this line: status = response.statuses[i];

You don't need to rename your variable because you're not in global scope, as long as you declare it with the var keyword: var status = response.statuses[i];

Upvotes: 0

Sune Trudslev
Sune Trudslev

Reputation: 974

Try this:

response = eval('({"success":true,"statuses":[{"title":"Unknown","code":"UNKNOWN","cssClass":"sys_unknown","id":1}]})');

var s = response.statuses;
for (var i=0,len = s.length;i<len;i++) {
    var st = s[i];
     if (typeof st.id != 'undefined') {
        alert('ID = ' + st.id);
     }
}

Upvotes: 1

Luca Rainone
Luca Rainone

Reputation: 16458

status is a reserved keyword in IE. Try to rename your var.

Upvotes: 0

Related Questions