Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:52:54+00:00 2026-06-17T11:52:54+00:00

I’m trying to create a file drag/drop handler (drag a file into the browser

  • 0

I’m trying to create a file drag/drop handler (drag a file into the browser window, to be used for upload).

For some reason when I bind the drag/drop listener to $("body") instead of to a $("div") in the body the events fire several times in a row, sometimes even non-stop (seemingly looping). What could be causing this?

Here’s a trimmed down version of the code: http://jsfiddle.net/WxMwK/9/

var over = false;

$("body")
    .on("dragover", function(e){
        e.preventDefault();
        if (! over) {
            over = true;
            $("ul").append($("<li/>").text("dragover"));    
        }
    })
    .on("dragleave", function(e){
        e.preventDefault();
        if (over) {
            over = false;
            $("ul").append($("<li/>").text("dragleave"));
        }
    })
    .on("drop", function(e){
        e.preventDefault();
        if (over) {
            over = false;
            $("ul").append($("<li/>").text("drop"));
        }
    }); 

To test: drag a file into the orange area, you’ll see the event firing multiple times in a row.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:52:56+00:00Added an answer on June 17, 2026 at 11:52 am

    The anon is (mostly) correct. To put it simply: when the mouse moves over the edge of an element inside your drop target, you get a dropenter for the element under the cursor and a dropleave for the element that was under the cursor previously. This happens for absolutely any descendant.

    You can’t check the element associated with dragleave, because if you move the mouse from your drop target onto a child element, you’ll get a dropenter for the child and then a dropleave for the target! It’s kind of ridiculous and I don’t see how this is a useful design at all.

    Here’s a crappy jQuery-based solution I came up with some time ago.

    var $drop_target = $(document.body);
    var within_enter = false;
    
    $drop_target.bind('dragenter', function(evt) {
        // Default behavior is to deny a drop, so this will allow it
        evt.preventDefault();
    
        within_enter = true;
        setTimeout(function() { within_enter = false; }, 0);
    
        // This is the part that makes the drop area light up
        $(this).addClass('js-dropzone');
    });
    $drop_target.bind('dragover', function(evt) {
        // Same as above
        evt.preventDefault();
    });
    $drop_target.bind('dragleave', function(evt) {
        if (! within_enter) {
            // And this makes it un-light-up  :)
            $(this).removeClass('js-dropzone');
        }
        within_enter = false;
    });
    
    // Handle the actual drop effect
    $drop_target.bind('drop', function(evt) {
        // Be sure to reset your state down here
        $(this).removeClass('js-dropzone');
        within_enter = false;
    
        evt.preventDefault();
    
        do_whatever(evt.originalEvent.dataTransfer.files);
    });
    

    The trick relies on two facts:

    • When you move the mouse from a grandchild into a child, both dragenter and dragleave will be queued up for the target element—in that order.
    • The dragenter and dragleave are queued together.

    So here’s what happens.

    • In the dragenter event, I set some shared variable to indicate that the drag movement hasn’t finished resolving yet.
    • I use setTimeout with a delay of zero to immediately change that variable back.
    • But! Because the two events are queued at the exact same time, the browser won’t run any scheduled functions until both events have finished resolving. So the next thing that happens is dragleave‘s event handler.
    • If dragleave sees that it was paired with a dragenter on the same target element, that means the mouse must have moved from some descendant to some other descendant. Otherwise, the mouse is actually leaving the target element.
    • Then the setTimeout finally resolves zero seconds later, setting back the variable before another event can come along.

    I can’t think of a simpler approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.