JavaScript does not support indexof how to solve _ javascript skills

Source: Internet
Author: User
The indexOf () method returns the position of the first occurrence of a specified string value. This article introduces how ie8 does not support indexof. IndexOf () method definition and usage

The indexOf () method returns the position of the first occurrence of a specified string value.

This method retrieves the stringObject string from start to end to see if it contains the searchvalue. The start position is at the string's fromindex or the start of the string (when fromindex is not specified ). If a searchvalue is found, the first position of the searchvalue is returned.

The character position in stringObject starts from 0.

If the string is not found in the array,-1 is returned.

Enter the subject:

The indexof method in js looks for the first index value that can be found in the array for a given element, but indexof is not supported in IE8. This article introduces the solution that ie8 does not support indexof.

How does a browser not support indexof? When writing a scripts, you can use the following code at the beginning, which allows you to use the indexOf method without local support.

if (!Array.prototype.indexOf) {Array.prototype.indexOf = function(searchElement, fromIndex) {var k;if (this == null) {throw new TypeError('"this" is null or not defined');}var O = Object(this);var len = O.length >>> 0;if (len === 0) {return -1;}var n = +fromIndex || 0;if (Math.abs(n) === Infinity) {n = 0;}if (n >= len) {return -1;}k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);while (k < len) {if (k in O && O[k] === searchElement) {return k;}k++;}return -1;};}

Js does not support indexof. This is the end of the introduction. The above solution is very useful. If you need it, please refer to the above tutorial. Thank you very much for your support for the script home website!

Related Article

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.