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]--> |