Skip to content

Conversation

@0xsatoshi99
Copy link
Contributor

Summary

Fixes the intermittent file detection issue in Firefox reported in the 6-year-old issue #45.

Problem

The file drag-and-drop example was inconsistently detecting files in Firefox:

  • Sometimes files were detected and names printed
  • Sometimes the drop event fired but no files were detected
  • Chrome worked consistently

From the issue:

File(s) dropped
File(s) dropped
File(s) dropped
File(s) dropped
... file[0].name = screen_fluoread.png  // Only worked once out of 4 tries

Root Cause

The removeDragData() function was attempting to call dataTransfer.items.clear() during the drop event:

function removeDragData(ev) {
  if (ev.dataTransfer.items) {
    ev.dataTransfer.items.clear();  // ❌ Read-only during drop!
  }
}

The dataTransfer object is read-only during the drop event, causing this operation to fail silently in Firefox, leading to inconsistent behavior.

Solution

1. Removed problematic removeDragData() function

  • No longer attempts to clear read-only dataTransfer
  • Simplified code flow

2. Added explicit dropEffect

function dragOverHandler(ev) {
  ev.preventDefault();
  ev.dataTransfer.dropEffect = 'copy';  // ✅ Explicit drop effect
}

3. Added dragLeaveHandler

function dragLeaveHandler(ev) {
  console.log('File(s) left drop zone');
}

Provides complete drag lifecycle feedback.

Changes

File: drag-and-drop/File-drag.html

  • Removed: 12 lines (removeDragData function and call)
  • Added: 7 lines (dropEffect + dragLeaveHandler)
  • Net: -5 lines, cleaner code

Testing

Before: Inconsistent file detection in Firefox
After: Consistent file detection in both Firefox and Chrome

The fix ensures:

  • Files are always detected when dropped
  • No read-only property violations
  • Better cross-browser compatibility
  • Cleaner, more maintainable code

Benefits

  1. Fixes Firefox Bug: Consistent file detection
  2. Better UX: Explicit drop effect cursor
  3. Complete Lifecycle: dragLeave handler added
  4. Cleaner Code: Removed unnecessary function
  5. Cross-browser: Works in Firefox, Chrome, and others

Fixes #45

Fixes the intermittent file detection issue in Firefox reported in mdn#45.

Problems Fixed:
1. Removed removeDragData() function that was trying to clear
   dataTransfer.items during drop event (read-only property)
2. Added explicit dropEffect = 'copy' in dragOverHandler for
   better browser compatibility
3. Added ondragleave handler for complete drag lifecycle

Root Cause:
The removeDragData() function was attempting to call
dataTransfer.items.clear() during the drop event, which is not
allowed as dataTransfer is read-only during this phase. This
caused inconsistent behavior in Firefox.

Solution:
- Removed the problematic removeDragData() call
- Set dropEffect explicitly to 'copy' for consistent behavior
- Added dragLeaveHandler for better user feedback

Testing:
This fix ensures files are consistently detected in both
Firefox and Chrome when dragged from the OS file manager.

Fixes mdn#45
@0xsatoshi99 0xsatoshi99 requested a review from a team as a code owner October 31, 2025 03:33
@0xsatoshi99 0xsatoshi99 requested a review from pransh15 October 31, 2025 03:33
@pransh15 pransh15 requested review from caugner and pransh15 and removed request for caugner and pransh15 October 31, 2025 09:38
@pepelsbey pepelsbey requested review from pepelsbey and removed request for pransh15 November 3, 2025 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File-drag.html example not working

1 participant