views:

87

answers:

3

Like :

var result = eval('(' + response + ')');
var html = value = '';

for(item in result)
{

}

response is a json response.

It stops at for.. in IE8.

How to fix that?

EDIT

I got the same error when running:

result = [1,2,3];
for(item in result)
{
...
}
A: 

This should not be the case with IE8; there should be some other reason.

Sarfraz
+2  A: 

I tested the code from JavaScript For...In Statement in IE8, no issue.

Definitely not an issue of the loop (not working in IE8) but what is in the 'result' object.

UPDATE:

I found the issue.

In IE8 (not sure about other IE versions) the word "item" somehow is a reserved word or something.

This will work:

var item;
for(item in result)
{
...
}

This will not (if item is not declared):

for(item in result)
{
...
}

This will work:

for(_item in result)
{
...
}
o.k.w
It turns out that it's because of lack of this statement:`var item;`.Have you met this?
See my findings in my update post. 'item' is indeed the culprit.
o.k.w
I'll never find out the truth by myself...
I would never have found out also if you didn't highlight the 'var item' in your comment :)
o.k.w
A: 

You should declare item explicitly with var. The standard idiom for using for..in is as follows and should only be used for iteration over objects (not arrays):

for ( var item in result ) {
    if ( !result.hasOwnProperty(item) ) {
        // loop body goes here
    }
}
Justin Johnson