IE7 Javascript and using string as an array - Stack Overflow

Weird behavior and I'm just posting this question to see if anyone knows the reason for this or wh

Weird behavior and I'm just posting this question to see if anyone knows the reason for this or whether my code is just plain wrong:

string text = "~"; //yip, let's take some weird character
alert(text[0]);
//all major browsers output "~"
//IE6 & 7: undefined
alert(text.charAt(0));
//works in all browsers

Now my question is: Is using a text as an array not supported in IE7, is the code wrong in general and is it OK to use .charAt(i) instead of string[i]?

PS: There is some guy who answered his own question regarding exactly this. My question remains: Where is this documented? Is this a regular IE "bug"?

Weird behavior and I'm just posting this question to see if anyone knows the reason for this or whether my code is just plain wrong:

string text = "~"; //yip, let's take some weird character
alert(text[0]);
//all major browsers output "~"
//IE6 & 7: undefined
alert(text.charAt(0));
//works in all browsers

Now my question is: Is using a text as an array not supported in IE7, is the code wrong in general and is it OK to use .charAt(i) instead of string[i]?

PS: There is some guy who answered his own question regarding exactly this. My question remains: Where is this documented? Is this a regular IE "bug"?

Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Dec 6, 2011 at 8:52 Dennis GDennis G 21.8k21 gold badges100 silver badges135 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Accessing string array-like is not standard in ECMAScript 3:

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/String#section_5

Array-like character access (the second way above) is not part of ECMAScript 3. It is a JavaScript and ECMAScript 5 feature.

What you do is split the string:

var textChars = text.split('');
alert(textChars[0]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744615721a4584039.html

相关推荐

  • IE7 Javascript and using string as an array - Stack Overflow

    Weird behavior and I'm just posting this question to see if anyone knows the reason for this or wh

    1天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信