Skip to content

Instantly share code, notes, and snippets.

@JonWatkins
Last active December 19, 2015 04:49
Show Gist options
  • Select an option

  • Save JonWatkins/5899780 to your computer and use it in GitHub Desktop.

Select an option

Save JonWatkins/5899780 to your computer and use it in GitHub Desktop.
Mobile file upload support detection
(function(a){
"use strict";
/**
* Mobile file upload support detection
* @constructor
*/
function FileSupport() {
// Support usage of the class without using the new keyword
if (!(this instanceof FileSupport)) {
return new FileSupport();
}
this.input = window.document.createElement('input');
this.input.type = "file";
this.input.display = "none";
this.supported = true;
this.errMessage = "Sorry your browser does not support uploading files.";
}
/**
* Runs regex on the navigation useragent to check for supported browsers
* @return {Boolean}
*/
FileSupport.prototype.isSupported = function() {
// Try to insert a file element into the page so we can check if the broweser
// is disabling the input
try {
window.document.getElementsByTagName("body")[0].appendChild(this.input);
if (this.input.disabled) {
this.supported = false;
} else {
this.supported = true;
}
}
// Set not supported if failed to create the element
catch(e) {
this.supported = false;
}
// Remove the element
finally {
this.input.parentNode.removeChild(this.input);
}
return this.supported;
};
/**
* Checks if the browser supports file uploads
* @param {Function} callBack
* @returns {Function}
*/
FileSupport.prototype.check = function(callBack) {
if (this.isSupported()) {
callBack(null);
} else {
callBack(new Error(this.errMessage));
}
};
// Attatch our library to the window object
if (!a.FileSupport) {
a.FileSupport = FileSupport;
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment