Home >Web Front-end >JS Tutorial >JavaScript does not support indexof. How to solve it_javascript tips
indexOf() method definition and usage
The indexOf() method returns the position of the first occurrence of a specified string value in the string.
This method will retrieve the string stringObject from beginning to end to see if it contains the substring searchvalue. The starting position of the search is at the fromindex of the string or the beginning of the string (when fromindex is not specified). If a searchvalue is found, the position of the first occurrence of searchvalue is returned.
Character positions in stringObject start from 0.
Returns -1 if the string is not found in the array.
Getting to the point:
The indexof method in js can find the first index value found in the array for a given element, but indexof is not supported in IE8. This article will introduce to you the solution for IE8 not supporting indexof
If a browser does not support indexof, you can use the following code at the beginning of scripts when writing them. It will allow you to use the indexOf method without native 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 solutions are very effective. Friends in need can refer to the above tutorials. At the same time, we are also very grateful for your support of the Script House website!