4 ms·
About prevent default: You need to tell the browser that this element will handle the drop/has handled the drop, or it will perform its default operation. What
by panzi 12y ago
About prevent default: You need to tell the browser that this element will handle the drop/has handled the drop, or it will perform its default operation. What is it's default operation? To load the file/URL that you just have dropped onto the browser window.
Or maybe you have multiple drop areas stacked on each other. Should the event propagate to the parent element or not?
Ok, why is it done in JavaScript and not simply with dropzone="true"? Because you want to be able to let script logic decide if that drop/dragover event is eaten or not.
I stopped reading at some point, but I wonder if the author has ever implemented D'n'D support in any other language. It works very similar to this in I guess most GUI APIs. Maybe that is another reason why it works like that in HTML5?
- eridius 12y agoI sympathize with your argument, but unfortunately it doesn't seem to be correct. Arguably, you should be able to implement just the 'drop' event and call event.preventDefault() in that event to suppress the default behavior of loading the file/URL. I don't see a clear reason why any other event should matter, the actual action that triggers the load is dropping. As for propagation to parent elements, that's not controlled by event.preventDefault(), that's controlled by event.stopPropagation(). I do agree that the author doesn't seem to realize that drag & drop is actually rather complex and that other languages look similar (e.g. large number of events). But the quirks around when you have to call preventDefault() do seem to be legitimate issues.
- panzi 12y agoYeah, in other languages you don't use "preventDefault" but e.g. "acceptProposedAction". So they could have added such an method to the event object. I guess it's a bit of a quick fix thing why Microsoft didn't do that and the w3/whatwg just took that. As to "I don't see a clear reason why any other event should matter". Well, yeah, one could implement it so that only the drop event really matters, but I guess it's done the way it is so it forces the programmer to explicitly handle the dragover event. This way the appropriate mouse icon will be displayed and the user knows what's going to happen when they drop now. Strange situation where I, a Linux user, defend something made by Microsoft. Usually I spew a lot of hate against Microsoft just for committing the crime called Internet Explorer (especially version 8, which I still have to support, bah).