0

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

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
LostInCyberSpace
  • 413
  • 4
  • 19

1 Answers1

1

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.

HBP
  • 15,685
  • 6
  • 28
  • 34
  • makes sense dude, it appears to have nothing to iterate.. Am aware of the `.split()` conversion just didn't know why it was happening. No doubt i've updated ie since coding in it, but get this.. i've just added the following line of code to the above `d1.innerText=cha:{str[0]` and lo-an-behold it displays `cha:{undefined}` in IE? Would this fall under the same indexing rules – LostInCyberSpace Jan 30 '15 at 11:27