Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable Firefox's default drag and drop on all images behavior with jQuery?

Firefox has this annoying behavior that it let's the user drag and drop any image element by default. How can I cleanly disable this default behavior with jQuery?

like image 563
Void Avatar asked Oct 06 '10 14:10

Void


2 Answers

The following will do it in Firefox 3 and later:

$(document).on("dragstart", function() {
     return false;
});

If you would prefer not to disable all drags (e.g. you may wish to still allow users to drag links to their link toolbar), you could make sure only <img> element drags are prevented:

$(document).on("dragstart", function(e) {
     if (e.target.nodeName.toUpperCase() == "IMG") {
         return false;
     }
});

Bear in mind that this will allow images within links to be dragged.

like image 153
Tim Down Avatar answered Nov 09 '22 23:11

Tim Down


If Javascript is an optional requirement, you can try with CSS

.wrapper img {
    pointer-events: none;
}
like image 22
José Antonio Avatar answered Nov 10 '22 00:11

José Antonio