Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 631
Matic-C Avatar asked Oct 30 '14 13:10

Matic-C


2 Answers

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
}
like image 105
Michael Avatar answered Sep 19 '22 16:09

Michael


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'
}
like image 41
Umair Khan Avatar answered Sep 18 '22 16:09

Umair Khan