Why does `mystring [0]` not return anything in IE?

So, in my javascript, I have the following code:

    var wholeHash = window.location.hash.substring(1);
    var data = new Object();

    // Remove the bang or slash if one appears at the beginning
    if (wholeHash[0] == '!') { wholeHash = wholeHash.substring(1); }
    if (wholeHash[0] == '/') { wholeHash = wholeHash.substring(1); }

When it starts, wholeHashit matters "/search/&&stype=quick". However, wholeHash[0]it does not return anything, which leads to an error wholeHash[0] == '!'. This is only in IE.

Why is this? I know I can use it instead startswith, but I usually wonder why IE cannot get individual characters of a string, while other browsers can.

+3
source share
3 answers

Since indexing into strings with array-style indexing is new, and older versions of IE do not have enough. Instead, you will need to use mystring.charAt(0)it if you need to support IE up to 8.

+6

. . .

+2

The "correct" way to get a character from a string is mystring.charAt(x).

However, you can split the string into an array with mystring.split("").

To you which you prefer.

+1
source

All Articles