IE8/IE9 in JavaScript does not support Array.indexof solutions

Source: Internet
Author: User


Scene:
JS determines whether a string is in an array of JS.
Method:

The code is as follows Copy Code

if ([' A ', ' B ', ' C '].indexof (str) >= 0) {
Do something
}

Determines whether the name of the selected file has an extension in the specified array:

The code is as follows Copy Code

var position = Selectedfilepath.lastindexof (".");
if (position<=0) {
Alert ("Do not allow uploading of files of this extension!") ");
Return
} else{
var ext = selectedfilepath.substr (position);
ext = Ext.tolowercase ();
var Extarr = [". jpg", ". png", ". gif", ". jpeg"];
if (extarr.indexof (EXT) = =-1) {
Alert ("Do not allow uploading of files of this extension!") ");
Return
}
}

Issue: IE9 The following version does not support [].indexof ()
Solution

Create your own indexof functions for IE8 and the following versions.
Ie8.js:

The code is as follows Copy Code

if (! Array.prototype.indexOf)
{
  Array.prototype.indexOf = function (ELT/*, from*/)
  {
 & nbsp;  var len = this.length >>> 0;

    var from = number (Arguments[1]) | | 0;
    from = (from < 0)
        ? Math.ceil (from)
        : Math.floor (from);
    if ( From < 0)
      from = len;

    for (; from < Len; from++)
    {
      if (from I n this &&
          this[from] = = ELT)
         return from;
   }
    return-1;
 };
}

Then introduce ie8.js in the relevant pages

The code is as follows Copy Code

<!--[If Lt IE 9]>
<script src= "Https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js" ></script>
<script src= "Https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js" ></script>
<script src= "./assets/js/ie8.js" ></script>
<! [endif]-->

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.