Safari 中的 Javascript FileReader 检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:33:18  来源:igfitidea点击:

Javascript FileReader detection in Safari

javascriptsafarifilereaderreferenceerror

提问by barry

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,

我知道 FileReader 对象在 Safari 5.0.5 中不可用。我有一个使用它的脚本,并认为我只能检测对象是否存在以运行一些替代代码,如此处建议的那样,

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

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.

问题是,我已经在 Safari 中对其进行了测试,一旦它遇到 if 语句,我就会收到此错误并且脚本停止运行。

ReferenceError: Can't find variable: FileReader

参考错误:找不到变量:FileReader

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

那么显然这不是最好的处理方式吗?知道为什么这不起作用吗?

回答by Assaf Moldavsky

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.

如果您真的想要细化和挑剔,请检查类型。

回答by SLaks

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

你可以写 if (typeof FileReader !== "undefined")

You can also use the Modernizrlibrary to check for you.

您还可以使用Modernizr库来检查您。

回答by Daniel

Or you can do something like this.

或者你可以做这样的事情。

if('FileReader' in window) {
    // FileReader support is available
} else {
    // No support available
}