LostInCyberSpace
LostInCyberSpace

Reputation: 423

for...in looping working in Chrome, but not in IE?

Ok peep's here's the problem...


This is a simplified version of a file I'm writing...

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>_</title>
        <script src="/sifu/query/jquery.js"></script>
        <script>
            /********************************************************/
            str='abcdefghijklmnopqrstuvwxyz'
            /********************************************************/
            function pop(){
                /****************************/
                sx='for(ia in str)'+'\n';
                for(ia in str)
                    sx+='\n'+ia+'   => '+str[ia];
                enX.innerText=sx;
                /****************************/
                sy='for(ib=0;ib<str.length;ib++)'+'\n';
                for(ib=0;ib<str.length;ib++)
                    sy+='\n'+ib+'   => '+str[ib];
                enY.innerText=sy;
                /****************************/
            }
            /********************************************************/
            $(document).ready(function(){
                pop()
            })
            /********************************************************/
        </script>
    </head>
    <body>
        <table style="width:100%;text-align:center">
            <tr>
                <td id="enX">enX</td>
                <td id="enY">enY</td>
            </tr>
        </table>
    </body>
</html>

Demo

str = 'abcdefghijklmnopqrstuvwxyz'

function pop() {
  sx = 'for(ia in str)' + '\n';
  for (ia in str) {
    sx += '\n' + ia + '	=> ' + str[ia];
  }
  enX.innerText = sx;

  sy = 'for(ib=0;ib<str.length;ib++)' + '\n';
  for (ib = 0; ib < str.length; ib++) {
    sy += '\n' + ib + '	=> ' + str[ib];
  }
  enY.innerText = sy;
}

$(document).ready(function() {
  pop()
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%;text-align:center">
  <tr>
    <td id="enX">enX</td>
    <td id="enY">enY</td>
  </tr>
</table>


When I load it in chrome it works fine... I get two equal columns of 0-25|a-z.


Yet, when I Load it in IE,

Column X, only has the header for(ia in str) and contains no instances of the loop ie: 0 => a etc...

And

Column Y, contains both the header for(ib=0;ib<str.length;ib++) and 26 instances of # => undefined, where # is the loop number.



I'm sorry to ask such a simplistic question peep's but I've recently been teaching myself how to code a web page specifically for chrome, leaving IE behind, and it seems that in the time it's taken to learn how to code in chrome I've forgotten the fundamentals of coding in IE...


All pointers welcome... just explain where I'm going wrong plz

Upvotes: 0

Views: 60

Answers (1)

HBP
HBP

Reputation: 16033

I can't find the reference but I am fairly sure that some versions of IE do not support indexing strings, you are supposed to use the the charAt function. That being true, in those versions of IE, the for .. in has nothing to iterate over for a string.

By converting your string to an array using .split('') and using for .. in on that the code should work in all browsers.

Upvotes: 1

Related Questions