Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if <input type="file" /> is supported

Tags:

I'm building a mobile version of my site which has a file upload facility, accessed via an 'Upload Button'

I would like to hide the button from iPhone users, as the control just appears greyed out - is this possible?

I don't really want to detect the iPhone; I feel it would be much better to detect the feature - making it start to work automatically should Apple enable this (or the phone is Jailbroken, or something...)

like image 787
Ben Robinson Avatar asked Nov 10 '11 10:11

Ben Robinson


People also ask

How do you check input type file is empty or not?

We get our input element with document. getElementById() and check the length of the files property, which an input of type file has. If it's empty, we alert the user that no file has been selected.

How can you tell if a file is canceled and clicked on input?

The property value type=”file” tells that the type of input the user enters is a file. The property value id=”theFile” is used to link the JavaScript code to it using getElementById() method. The property value onclick=”initialize()” is used to specify the function call when the user has clicked on the input.


1 Answers

Function to check whether input[type=file] is implemented:

function isInputTypeFileImplemented() {     var elem = document.createElement("input");     elem.type = "file";     if (elem.disabled) return false;     try {         elem.value = "Test"; // Throws error if type=file is implemented         return elem.value != "Test";     } catch(e) {         return elem.type == "file";     } } 

Fiddle: http://jsfiddle.net/8EqEE/9

like image 135
Rob W Avatar answered Oct 29 '22 04:10

Rob W