Is it possible to disable the drag-and-drop functionality on elements which have the contentEditable attribute set to true.
I have the following HTML page.
<!DOCTYPE html>
<html><meta charset="utf-8"><head><title>ContentEditable</title></head>
<body>
<div contenteditable="true">This is editable content</div>
<span>This is not editable content</span>
<img src="bookmark.png" title="Click to do foo" onclick= "foo()">
</span>
</body>
</html>
The main problem I'm facing is that it is possible to drag and drop the image into the DIV and it gets copied (along with the title and the click handler)
I have found that I need to set the dropEffect to 'none' for both dragenter and dragover to reject a drag. If you don't bind dragenter, the cursor will flicker when the drag enters the element, so the code would be:
Would it be possible to disable all mousedown events on the image?
Interesting, I don't know the drag and drop interface well enough to be sure, but it looks like there's some sort of bug here. I modified your code slightly to add a handler for the drop event on the editable div:
I'm testing in Firefox 3.7 alpha: if I drag the image on to the middle of the text the event fires, I get an alert, and the
return false
stops the image being dropped. However if I drag on to the start of the text the event doesn't fire but the image gets inserted inside the div. Mind you, in Firefox 3.6 and Chromium 6.0 the event doesn't fire at all, so either I've completely misunderstood how it works or none of it works well enough right now to rely on.In Chrome and Firefox, you have to cancel the
ondragover
event for theondrag
event to fire. I would also recommend canceling theondragenter
andondragleave
events just to be sure.http://jsbin.com/itugu/2
This behavior isn't a bug; in the HTML5 specification it says that
ondragover
must be canceled forondrag
to fire. It doesn't make any sense to do it this way, so I hope they change it soon. See http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.htmlThe following snippet will prevent your div from receiving dragged content:
I (and several others) find this api to be strange and counter-intuitive, but it does prevent a contenteditable from receiving dragged content in all browsers except IE8 (including IE9 beta). As of yet, I cannot prevent IE8 from accepting contenteditable.
I'll update this answer if I find a way to do so.