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 7846105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:28:02+00:00 2026-06-02T17:28:02+00:00

Firefox doesn’t properly trigger the dragleave event when dragging outside of the window: https://bugzilla.mozilla.org/show_bug.cgi?id=665704

  • 0

Firefox doesn’t properly trigger the dragleave event when dragging outside of the window:

https://bugzilla.mozilla.org/show_bug.cgi?id=665704

https://bugzilla.mozilla.org/show_bug.cgi?id=656164

I’m trying to develop a workaround for this (which I know is possible because Gmail is doing it), but the only thing I can come up with seems really hackish.

One way of knowing when dragging outside the window has occurred it to wait for the dragover event to stop firing (because dragover fires constantly during a drag and drop operation). Here’s how I’m doing that:

var timeout;

function dragleaveFunctionality() {
  // do stuff
}

function firefoxTimeoutHack() {
  clearTimeout(timeout);
  timeout = setTimeout(dragleaveFunctionality, 200);
}

$(document).on('dragover', firefoxTimeoutHack);

This code is essentially creating and clearing a timeout over and over again. The 200 millisecond timeout will not be reached unless the dragover event stops firing.

While this works, I don’t like the idea of using a timeout for this purpose. It feels wrong. It also means there’s a slight lag before the “dropzone” styling goes away.

The other idea I had was to detect when the mouse leaves the window, but the normal ways of doing that don’t seem to work during drag and drop operations.

Does anyone out there have a better way of doing this?

UPDATE:

Here’s the code I am using:

 $(function() {
          var counter = 0;
          $(document).on('dragenter', function(e) {
            counter += 1;
            console.log(counter, e.target);
          });
          $(document).on('dragleave', function(e) {
            counter -= 1;
            console.log(counter, e.target);
          });
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Open up the console and look at what number is reporting when dragging files in and out of the window. The number should always be 0 when leaving the window, but in Firefox it's not.</p>
  • 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-02T17:28:03+00:00Added an answer on June 2, 2026 at 5:28 pm

    I’ve found a solution. The problem was not so much that the dragleave event wasn’t firing; rather, the dragenter event was firing twice when first dragging a file into the window (and additionally sometimes when dragging over certain elements). My original solution was to use a counter to track when the final dragleave event was occuring, but the double firing of dragenter events was messing up the count. (Why couldn’t I just listen for dragleave you ask? Well, because dragleave functions very similarly to mouseout in that it fires not only when leaving the element but also when entering a child element. Thus, when dragleave fires, your mouse may very well still be within the bounds of the original element.)

    The solution I came up with was to keep track of which elements dragenter and dragleave had been triggered on. Since events propagate up to the document, listening for dragenter and dragleave on a particular element will capture not only events on that element but also events on its children.

    So, I created a jQuery collection $() to keep track of what events were fired on what elements. I added the event.target to the collection whenever dragenter was fired, and I removed event.target from the collection whenever dragleave happened. The idea was that if the collection were empty it would mean I had actually left the original element because if I were entering a child element instead, at least one element (the child) would still be in the jQuery collection. Lastly, when the drop event is fired, I want to reset the collection to empty, so it’s ready to go when the next dragenter event occurs.

    jQuery also saves a lot of extra work because it automatically does duplicate checking, so event.target doesn’t get added twice, even when Firefox was incorrectly double-invoking dragenter.

    Phew, anyway, here’s a basic version of the code I ended up using. I’ve put it into a simple jQuery plugin if anyone else is interested in using it. Basically, you call .draghover on any element, and draghoverstart is triggered when first dragging into the element, and draghoverend is triggered once the drag has actually left it.

    // The plugin code
    $.fn.draghover = function(options) {
      return this.each(function() {
    
        var collection = $(),
            self = $(this);
    
        self.on('dragenter', function(e) {
          if (collection.length === 0) {
            self.trigger('draghoverstart');
          }
          collection = collection.add(e.target);
        });
    
        self.on('dragleave drop', function(e) {
          collection = collection.not(e.target);
          if (collection.length === 0) {
            self.trigger('draghoverend');
          }
        });
      });
    };
    
    // Now that we have a plugin, we can listen for the new events 
    $(window).draghover().on({
      'draghoverstart': function() {
        console.log('A file has been dragged into the window.');
      },
      'draghoverend': function() {
        console.log('A file has been dragged out of window.');
      }
    });
    

    Without jQuery

    To handle this without jQuery you can do something like this:

    // I want to handle drag leaving on the document
    let count = 0
    onDragEnter = (event) => {
      if (event.currentTarget === document) {
        count += 1
      }
    }
    
    onDragLeave = (event) => {
      if (event.currentTarget === document) {
         count += 0
      }
    
      if (count === 0) {
        // Handle drag leave.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firefox doesn't know how to open this address because the protocol (c) isn't associated
The following CSS works well under firefox but doesn't work under IE browser, Why?
Valumes doesn't show UPLOAD button under Firefox (MVC3 Project). It works fine under: IE,
Why doesn't following code work correctly in FireFox 3.6? I have tested in IE7,
The following doesn't work... (at least not in Firefox: document.getElementById('linkid').click() is not a function)
I'm using Firefox and I've been reading some forums claiming this doesn't work in
Why doesn't this work <script src=jquery.js/> But this works <script src=jquery.js></script> ? Firefox 3.5.8
In Firefox it works, in my Internet Explorer 6 or 7 it doesn't: <html>
The last log in the following code doesn't work in Firefox. Why? (function() {
So firefox doesn't want to load my fonts, the path is right I also

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.