检查当前浏览器是否支持JS API函数

Check if JS API function is supported in a current browser

本文关键字:JS API 函数 支持 是否 浏览器 检查      更新时间:2023-09-26

我有一个例子,需要检查Safari V 5.1是否支持FileReader功能。我尝试过:

if (typeof FileReader !== "object") { 
    alert("NA");
}

然而,现在即使在我知道支持FileReader的其他浏览器中,我也会显示警报!所以我想我一定做错了什么。

检查是否定义了函数:

你试过下面的吗?

if(typeof(window.FileReader)!="undefined"){
     //Your code if supported
}else{
     //your code if not supported
}

来自MDN

window对象的window属性指向窗口对象本身。

可以使用JS IN运算符。

if('FileReader' in window)
  console.log('FileReader found');
else
  console.log('FileReader not found');

或使用给定的代码示例。

if (!'FileReader' in window) {
  alert("NA"); // alert will show if 'FileReader' does not exists in 'window'
}