Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect browser support of html file input element

Is there a way with a javascript/jquery/crazy-css-hack to detect if the user's browser supports rendering a functional <input type="file" /> file upload element? For example, the safari browser on iOS won't render the element and I would like to instead show a message to the user that the functionality isn't supported. I know I can inspect the user agent and check to see if it an iphone/ipad/etc but I don't know what other browsers do or do not support it or will or will not support it in the future.

like image 949
bkaid Avatar asked Nov 08 '10 20:11

bkaid


People also ask

How do I know my browser support HTML5?

The getContext method is checked by accessing it on the created input object. The result of this expression is checked with an if-statement. If the result is true, it means that HTML5 is supported by the browser.

How do you get input from a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!


1 Answers

Galambalazs' answer pointed me in the right direction for iOS only. I ended up using this:

function supportsFileInput() {
  var dummy = document.createElement("input");
  dummy.setAttribute("type", "file");
  return dummy.disabled === false;
}

However, it doesn't work for most Android devices, as this function always returns true but renders the button with the text "Uploads disabled".

like image 57
bkaid Avatar answered Oct 05 '22 02:10

bkaid