8

Can we detect whether a browser supports dropping a file over an <input type="file" />?

For example, this is possible in Chrome but not in IE8.

Modernizr.draganddrop is a possibility but is it the right choice? I'm not adding any custom drag/drop event handlers.

Update

To verify Joe's answer here's a jQuery example that should stop the file drop. Verified in Chrome and Firefox.

$yourFileInput.on('drop', function() {
    return false; // if Joe's explanation was right, file will not be dropped
});
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Danyal Aytekin
  • 4,106
  • 3
  • 36
  • 44

1 Answers1

2

I think the answer to Detecting support for a given JavaScript event? may help you. Adjusting the code to test against Input instead of Div, and testing for the "Drop" event seems to work fine for me.

Reproduced here so you don't have to click through (and adjusted slightly, since it seems you only need to detect this one feature):

function isEventSupported(eventName) {
  var el = document.createElement('input');
  eventName = 'on' + eventName;
  var isSupported = (eventName in el);
  if (!isSupported) {
    el.setAttribute(eventName, 'return;');
    isSupported = typeof el[eventName] == 'function';
  }
  el = null;
  return isSupported;
}
if(isEventSupported('drop')){
  //Browser supports dropping a file onto Input
} else {
  //Fall back, men!
}
Community
  • 1
  • 1
joequincy
  • 1,395
  • 10
  • 21
  • This is a nice approach. Are you convinced that these browsers actually use the standard 'drop' event for files being dropped over the input, rather than something home-baked? – Danyal Aytekin Oct 29 '12 at 10:51
  • Great, I've confirmed that you're right, by returning 'false' from the drop event and observing that the drop doesn't work. Thanks Joe. – Danyal Aytekin Oct 29 '12 at 10:58
  • Always welcome! Also suggest upvoting the answer on the other page, because the approach belongs to @kangax and I only found it because of that post :) – joequincy Oct 29 '12 at 15:17