Test if dragenter/dragover Event Contains Files

HTML5 drag and drop is great for handling file uploads. But if that's the only thing you are using it for, it's nice to know if any particular dragenter or dragover event actually has files. Unlike, for example, just the dragging of some selected text.

Send the event object to this function and it will return the truth (assuming you are in a browser that supports all this):

function containsFiles(event) {

    if (event.dataTransfer.types) {
        for (var i = 0; i < event.dataTransfer.types.length; i++) {
            if (event.dataTransfer.types[i] == "Files") {
                return true;
    return false;


Reference URL


  1. dgriesel
    Permalink to comment#

    How about this?

    if ( t = event.dataTransfer.types )
        return t.contains("Files")
    • tbruun
      Permalink to comment#

      Beware that types in Firefox is a DOMStringList, while in Chrome it’s an Array. You can use types.indexOf("Files") !== -1, but that won’t work with the DOMStringList. At this point you might as well go with the author’s solution.

  2. Jeff Ayer
    Permalink to comment#

    A different ie9+ take:

    containsFiles = function(e) {
        var dataType = Array.prototype.slice.call(e.dataTransfer.types);
        dataType = dataType.filter(function(e) {
            return e == "Files";
        return dataType.length;
  3. Vclav

    Simpler and more efficient version:

    isEventWithFiles(event) {
        var temp = (event.originalEvent || event).dataTransfer;
        return temp && (temp = temp.types) && temp[0] === 'Files';

    PS: works with any event, even with jQuery Event object.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.