Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between event.originalEvent.dataTransfer.files and event.dataTransfer.files in html 5

What is the difference between

event.originalEvent.dataTransfer.files

and

event.dataTransfer.files

???

Because in drag and drop , second code does not work and it is undefined and i had to use first code because it works !

like image 270
peyman gilmour Avatar asked Aug 09 '12 14:08

peyman gilmour


1 Answers

jQuery does not pass the browser event object to you, it passes the jQuery event object. You can access the original untouched event object by the .originalEvent property in the jQuery event object. Then you can proceed as normal.

So in code:

jQuery event model:

$(document).on("drop dragend dragstart dragenter dragleave drag dragover", function(event) {
    event.preventDefault();
    if (event.type === "drop") {
        alert(event.originalEvent.dataTransfer.files);
    }
});

W3C event model:

function preventDefault(event) {
    event.preventDefault();
}

function alertFiles(event) {
    alert(event.dataTransfer.files);
}
document.addEventListener("dragstart", preventDefault, false);
document.addEventListener("dragenter", preventDefault, false);
document.addEventListener("dragleave", preventDefault, false);
document.addEventListener("drag", preventDefault, false);
document.addEventListener("dragend", preventDefault, false);
document.addEventListener("dragover", preventDefault, false);
document.addEventListener("drop", preventDefault, false);
document.addEventListener("drop", alertFiles, false);
like image 78
Esailija Avatar answered Sep 27 '22 22:09

Esailija