Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't HTML5 drag and drop work in Firefox?

I have bound events to different elements, and when I drag them in all browsers, except Firefox, it works as expected. In firefox, however, it doesn't work at all. The only event that fires is dragstart, and none of the other events fire. What's going on?

like image 260
RandallB Avatar asked Sep 27 '13 16:09

RandallB


People also ask

Does HTML5 work with Firefox?

Firefox doesn't support HTML5 while other Browsers do.

Does HTML5 support drag-and-drop?

Now HTML 5 came up with a Drag and Drop (DnD) API that brings native DnD support to the browser making it much easier to code up. HTML 5 DnD is supported by all the major browsers like Chrome, Firefox 3.5 and Safari 4 etc.

How do you set dragged data in HTML5?

setData() The DataTransfer. setData() method sets the drag operation's drag data to the specified data and type. If data for the given type does not exist, it is added at the end of the drag data store, such that the last item in the types list will be the new type.


Video Answer


5 Answers

I'm not using jQuery, so removed the originalEvent portion and changed the format to text (or IE had issues), and it works:

event.dataTransfer.setData('text', 'anything');  

In the drop event make sure to call:

event.preventDefault();

Or it will jump to anything.com.

like image 76
Carson Holmes Avatar answered Oct 04 '22 09:10

Carson Holmes


Firefox requires that a user run the dataTransfer.setData function in the event.

For you jQuery users, that means the following code should resolve your issue:

function dragstartHandler(event){

  event.originalEvent.dataTransfer.setData('text/plain', 'anything');

}

Future events on the same drag will now properly fire as you expected. Obviously you can replace the setData arguments with more useful data.

like image 44
RandallB Avatar answered Oct 04 '22 09:10

RandallB


FF has long-standing issues with eating certain mouse events that originate from elements that have overflow set to auto or scroll.

In my case, I had a well-used library for drag and drop that worked perfectly in the samples and in my app on every browser but Firefox. After digging through the tickets related to this, I found a solution that I fully credit to a contributor to this ticket

which is to set

-moz-user-select: none

on the scrolled element being dragged from. It solved my particular problem.

like image 41
user3795410 Avatar answered Oct 04 '22 11:10

user3795410


You can use this as a reference for this question solution regarding the redirects that occur on Firefox.

You need to prevent the default action in drop method to fix this issue.

function drop(e) {
    if(e.preventDefault) { e.preventDefault(); }
    if(e.stopPropagation) { e.stopPropagation(); }

    //your code here

    return false;
}

I got this solution from this link.

like image 34
Akhilesh Sehgal Avatar answered Oct 04 '22 10:10

Akhilesh Sehgal


use this

IE:

event.dataTransfer.setData('text', '');

Firefox:

event.dataTransfer.setData('text/plain', '');
like image 41
bafuka Avatar answered Oct 04 '22 10:10

bafuka