views:

2716

answers:

2

I am writing an application that's supposed to support HTML5 drag/drop API for file, much like the on described here. I would like to perform a programmatic check on whether the browser support this kind of madness :) A solution that works for now is checking whether the browser provides a FileReader class, like this:

  if (typeof(FileReader) == "undefined") {
    $("#dropbox").hide();
  } else {
    // connect events
    $("#filebox").hide();
  }

But it is obviously not a correct one (I don't use that class at all).

Any ideas?

+3  A: 

Checking for the existence of FileReader is the correct way to go about this. FileReader is an official part of the File Api. I would combine this with Modernizr. Drag and Drop support is slated for release 1.2. You should be able to grab the source on GitHub and start working with this now. http://github.com/Modernizr/Modernizr/blob/master/modernizr.js

if (!!FileReader && Modernizr.draganddrop) {
  // sexy drag and drop action
} else {
  // no drag and drop support available :(
}

If you haven't seen Dive into HTML5, you should definitely check out Mark Pilgrim's suggestions on detecting HTML5.

dshaw
I'm not sure this would work. It checks for presence of attributes ondrag, ondrop, ondragleave etc. in a div object. This works in, say Chrome, but still doesn't let you drag-and-drop files into a website.
konryd
dshaw
Updated my suggestion to include check for the FileReader object, though if(!!FileReader) {} might be sufficient. :)I tested this in FF3.6 and Chrome and it works.
dshaw
Just realized that as I was thinking this through, you had mentioned that you thought evaluating for the presence of FileReader was incorrect. Even if you aren't going to be using the native implementation, FileReader is official spec and detecting for support of that feature is the right way to go about this. http://dev.w3.org/2006/webapi/FileAPI/#filereader-interface
dshaw
One last comment: If you're going to roll your own FileReader, you should make sure you implement the official interface. http://www.w3.org/TR/FileAPI/#dfn-filereaderThis way, you'll be able to drop your implementation once the FF implementation works the way you need it to work and other browsers begin to implement it.
dshaw
I'm almost sold to it, but it means, that whenever a browser implements FileReader API (which lets you read also from <inputs type=file>) it also implements drag'n'drop files from desktopinto the browser. Those are seperate functions. Does the spec say they need to come together?
konryd
This is enough:if ("files" in DataTransfer.prototype) {...}
Paul Rouget
@konryd I don't think they need to be, but I assumed that what you were doing would require both.You should really consider Paul's suggestion. Haven't tested it myself, but Paul's a Mozilla Tech Evangelist. http://twitter.com/paulrouget I'd defer to greater his experience.
dshaw
@konryd I looked it up: http://www.w3.org/TR/html5/editing.html#the-dragevent-and-datatransfer-interfaces
dshaw
+4  A: 
if ("files" in DataTransfer.prototype) {...}
Paul Rouget
it needs prototype.js though
konryd
No. prototype is a core JS programming construct.http://en.wikipedia.org/wiki/JavaScript
dshaw
@konryd See http://mckoss.com/jscript/object.htm for a better exploration of of JS object prototypes.
dshaw