最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - Old IE JavaScript doesn't support indexOf - Stack Overflow

programmeradmin4浏览0评论

From input file type i am passing fullPath(entire local path name) to javascript , and i have written javascript to know the file extension type ,

 while (fullPath.indexOf("\\") != -1)
            fullPath = fullPath.slice(file.indexOf("\\") + 1);
        alert(fullPath);

I have problem in IE only at above part , exactly i found indexOf is not supported in IE, how may i alter my this . If that is not the case is there any alternate to know the file extension which can work in all browsers.

thanks,
michaeld

From input file type i am passing fullPath(entire local path name) to javascript , and i have written javascript to know the file extension type ,

 while (fullPath.indexOf("\\") != -1)
            fullPath = fullPath.slice(file.indexOf("\\") + 1);
        alert(fullPath);

I have problem in IE only at above part , exactly i found indexOf is not supported in IE, how may i alter my this . If that is not the case is there any alternate to know the file extension which can work in all browsers.

thanks,
michaeld

Share Improve this question edited Jul 26, 2011 at 13:42 EricLaw 57.1k8 gold badges152 silver badges196 bronze badges asked Jul 26, 2011 at 11:08 michaelmichael 5852 gold badges14 silver badges27 bronze badges 1
  • For a thorough explanation of the issue as well as a work around not only for indexOf but the other missing array functions in IE check out the StackOverflow question stackoverflow./questions/2790001/… – Luis Perez Commented Jan 11, 2012 at 2:44
Add a ment  | 

2 Answers 2

Reset to default 6

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?

发布评论

评论列表(0)

  1. 暂无评论