Compatibility issues with the indexof () method of the javascript--array in IE8

Source: Internet
Author: User

There was a problem at work yesterday: the IndexOf () method of the array is not valid in IE8.

The following code in IE8 error " object does not support" indexOf "property or method ":

var arr = [n/a];

var index = Arr.indexof (3);

Workaround 1: Add ToString () to the string before calling the IndexOf method, and then call IndexOf (), but then we find that the index of 3 is not our expected 2, but 4, because after the conversion to the string is "a", When we look at index, the comma is also counted, obviously this is not the result we want. It's just that when you judge whether an object A is contained in an array of arr, you can use Arr.tostring (). IndexOf (a)!=-1 to determine, but this approach is not possible if you want to find the exact index of an object in the array arr.

Workaround 2: Use the $.inarray of the jquery framework (Find the object, find the array).

You can use this method to get the exact index of the object in the array, but be aware of the data type, when calling this method, jquery does not go to the automatic conversion type, the lookup object and the data object in the lookup array need to be the same type, otherwise the result is 1.

The above Method 1 is a colleague to give me the way, because then the demand is just to determine whether the object is in the array, the use of his introduction of Method 1; Method 2 is what I found on the Internet, recommended Method 2. Of course, indexof for arrays, in IE9 and above, WebKit kernel browser can be used, if you want to be compatible with IE8, you need to use the solution described above.

Compatibility issues with the indexof () method of the javascript--array in IE8

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.