Old IE JavaScript doesn't support indexOf

喜夏-厌秋 提交于 2019-12-02 08:47:35

You could create it (Javascript Code to create method)

For ease of use:

if(!Array.indexOf){
   Array.prototype.indexOf = function(obj){
       for(var i=0; i<this.length; i++){
          if(this[i]==obj){ 
             return i; 
          }
       } 
       return -1; 
     }
 }

indexOf() is supported in IE, at least as far back as version 3.0, assuming you are trying to use it on strings. I believe support for indexOf() on arrays was finally added in IE9.

The example you include in your question is using indexOf() on variables called fullPath and file, which I would assume are strings, but why are you mixing up a while condition on the index within fullpath with a slice operation that uses the index within file:

while (fullPath.indexOf("\\") != -1)
  fullPath = fullPath.slice(file.indexOf("\\") + 1);
// what is the file variable ^^

To work out the file type you want all the characters after the last ".", so try using the lastIndexOf() function:

var fileType = fullPath.slice(fullPath.lastIndexOf(".") + 1);

(Add your own else case for when there is no "." in the string.)

As an aside, I wouldn't assume that the filesystem uses the "\" character to separate folder names: what about non-Windows systems?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!