Matic-C
Matic-C

Reputation: 855

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.

Upvotes: 10

Views: 9663

Answers (2)

Umair Khan
Umair Khan

Reputation: 1752

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'
}

Upvotes: 4

Michael
Michael

Reputation: 496

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
}

Upvotes: 16

Related Questions