'Check if JS API function is supported in a current browser
I have an example where I need to check if Safari V 5.1 supports FileReader function. I tried with:
if (typeof FileReader !== "object") {
alert("NA");
}
However now even in my other browsers which I know for a fact they support FileReader I get the alert displayed! So I imagine I must be doing something wrong.
Solution 1:[1]
check if the function is defined or not:
have you tried the following?
if(typeof(window.FileReader)!="undefined"){
//Your code if supported
}else{
//your code if not supported
}
Solution 2:[2]
From MDN
The window property of a Window object points to the window object itself.
JS IN operator can be used.
if('FileReader' in window)
console.log('FileReader found');
else
console.log('FileReader not found');
OR using given code sample.
if (!'FileReader' in window) {
alert("NA"); // alert will show if 'FileReader' does not exists in 'window'
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Umair Khan |
