Old IE JavaScript doesn't support indexOf
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 abov开发者_运维技巧e 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,
michaeldYou 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?
精彩评论