I've the following: http://jsfiddle.net/KywJT/
function dragEnter(evt) {
evt.stopPropagation();
evt.preventDefault();
$(evt.target).addClass('over');
}
function dragLeave(evt) {
evt.stopPropagation();
evt.preventDefault();
$(evt.target).removeClass('over');
}
function drop(evt) {
evt.stopPropagation();
evt.preventDefault();
$(evt.target).removeClass('over');
}
jQuery( function ( $ ) {
var $box = $( "#box" );
$box.bind("dragenter", dragEnter);
$box.bind("dragleave", dragLeave);
$box.bind("drop", drop);
});
I'm using Chrome version 24.0.1312.52 m and last jQuery (1.8.3). When I drop a file into the box, browser is not preventing default beaviour. Can you please help me?
P.S. dragexit is deprecated correct?
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.
Most areas of a web page or application are not valid places to drop data. Thus, the default handling of these events is not to allow a drop. Calling the preventDefault() method during both a dragenter and dragover event will indicate that a drop is allowed at that location.
The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link.
The dragover event is fired when an element or text selection is being dragged over a valid drop target (every few hundred milliseconds). The event is fired on the drop target(s).
If you want the drop event to be fired in Google Chrome, a dragover listener has to be defined before, even if the function doesn't do anything at all. I don't know why, it's weird... but clearly identified and easy to reproduce :) Lost 2 hours on that sh*t yesterday :/ hope this helps
This should fix the issue for you.
jQuery( function ( $ ) {
var $box = $( "#box" );
$box.bind("dragenter", dragEnter);
$box.bind("dragleave", dragLeave);
$box.bind("drop", drop);
$(document).bind('dragover', function (e) {
e.preventDefault();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With