Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript FileReader detection in Safari

I'm aware of the fact that the FileReader Object is not available in Safari 5.0.5. I have a script that uses it and thought that i'd just be able to detect whether the object exists to run some alternate code, as is suggested here,

http://www.quirksmode.org/js/support.html

So my code is,

if( FileReader )
{
    //do this

}else{

    //the browser doesn't support the FileReader Object, so do this
}

The problem is, i've tested it in Safari and once it hits the if statement i get this error and the script stops running.

ReferenceError: Can't find variable: FileReader

So obviously that's not the best way to deal with it then? Any idea why this doesn't work?

like image 865
barry Avatar asked Jun 19 '11 16:06

barry


3 Answers

I believe in your case you can get away with a simpler check:

if(window.FileReader) {
   //do this
} else {
   //the browser doesn't support the FileReader Object, so do this
}

check for the type if you really wanna be granular and picky.

like image 197
Assaf Moldavsky Avatar answered Oct 21 '22 22:10

Assaf Moldavsky


You can write if (typeof FileReader !== "undefined")

You can also use the Modernizr library to check for you.

like image 35
SLaks Avatar answered Oct 21 '22 21:10

SLaks


Or you can do something like this.

if('FileReader' in window) {
    // FileReader support is available
} else {
    // No support available
}
like image 35
Daniel Avatar answered Oct 21 '22 20:10

Daniel