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

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.com/questions/2790001/… Commented Jan 11, 2012 at 2:44

2 Answers 2

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; 
     }
 }
Sign up to request clarification or add additional context in comments.

2 Comments

hi steve looked it but not clear how to add into the code , but solved it in my code other way. thank you!!!
While this is a suitable, simple replacement, the MDN has a more robust polyfill which is ECMA compliant. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.