Xaver
Xaver

Reputation: 11652

How to detect browser support File API drag n drop

I like to add a background on a div only for browsers which support drag & drop for files

I don't like to use modernizr though, just a one line script

Upvotes: 4

Views: 7724

Answers (6)

Pithikos
Pithikos

Reputation: 20300

Not sure why everybody needs to create a new element to check this. I think it's enough to just check that the body element supports dragging events and that the browser supports the File API

supportsDragAndDrop(){
   body = document.body
   return ("draggable" in body || ("ondragstart" in body && "ondrop" in body)) 
        && window.FormData && window.FileReader
}

Upvotes: 0

terrinecold
terrinecold

Reputation: 231

except this seems to incorrectly detect iOS as supporting drag and drop

Upvotes: 0

pashaplus
pashaplus

Reputation: 3706

checkout modernizr source code technique for the HTML5 drag and drop detection https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js

Upvotes: 0

German Latorre
German Latorre

Reputation: 11128

If you don't want to deal with Modernizr at all, you can just replicate what it does for drag'n'drop detection:

var supportsDragAndDrop = function() {
    var div = document.createElement('div');
    return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}

Got it from Modernizr GitHub repository:

https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js

Upvotes: 0

Yoggi
Yoggi

Reputation: 572

You can use:

return 'draggable' in document.createElement('span') && typeof(window.FileReader) != 'undefined';

Upvotes: 0

anton_byrna
anton_byrna

Reputation: 2555

Why not just copy required parts from Modernizr?

var isEventSupported = (function() {

      var TAGNAMES = {
        'select': 'input', 'change': 'input',
        'submit': 'form', 'reset': 'form',
        'error': 'img', 'load': 'img', 'abort': 'img'
      };

      function isEventSupported( eventName, element ) {

        element = element || document.createElement(TAGNAMES[eventName] || 'div');
        eventName = 'on' + eventName;

        // When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
        var isSupported = eventName in element;

        if ( !isSupported ) {
          // If it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
          if ( !element.setAttribute ) {
            element = document.createElement('div');
          }
          if ( element.setAttribute && element.removeAttribute ) {
            element.setAttribute(eventName, '');
            isSupported = typeof element[eventName] == 'function';

            // If property was created, "remove it" (by setting value to `undefined`)
            if ( typeof element[eventName] != 'undefined' ) {
              element[eventName] = undefined;
            }
            element.removeAttribute(eventName);
          }
        }

        element = null;
        return isSupported;
      }
      return isEventSupported;
    })();

Usage:

if (isEventSupported('dragstart') && isEventSupported('drop')) {
  ...
}

And for File API:

var isFileAPIEnabled = function() {
  return !!window.FileReader;
};

Upvotes: 8

Related Questions