3

I want to be able to disallow images (and other stuff) from being dropped into a content editable div across browsers. I see how to do it for Chrome and IE. Firefox stubbornly refuses. The code snippet at http://jsbin.com/oqozo3 has an example which shows that the event is firing, I am cancelling it, and in Firefox the image still appears.

Basically, The following fails to work in Firefox:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ContentEditable fails in FF</title>
<script type="text/javascript">
function init() {
  var target = document.getElementById("target");
  if (target.addEventListener) {
    target.addEventListener ("dragenter", all, false);
    target.addEventListener ("dragover", all, false);
    target.addEventListener ("drop", all, false);    
    target.addEventListener ("dragdrop", all, false);
  } else {
    target.attachEvent ("ondragenter", all, false);
    target.attachEvent ("ondragover", all, false);
    target.attachEvent ("ondrop", all, false);    
  }
}
function all(event) {
if (event.preventDefault) { event.preventDefault();} 
return false;
}
</script>
</head>
<body onLoad="init();">
    <div id="target" style="border: 1px solid black; height: 100px;" contenteditable="true">This is editable content</div>
    <div>
     <p>I don't want the image to be draggable to the above div. Works in IE,
     Chrome, not firefox.</p>
     <img src="http://mozcom-cdn.mozilla.net/img/firefox-100.jpg">
    </div>
</body>
</html>

I have already looked at: ContentEditable DIV - disabling drag and drop, but the suggestions there also don't work in Firefox.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
seth
  • 345
  • 1
  • 11

1 Answers1

0

any reason why you aren't using a framework like jQuery, it does stuff like this that is cross-browser

Cubed Eye
  • 5,581
  • 4
  • 48
  • 64
  • Actually, I am using Jquery, but there was nothing for this particular case that I thought I could use. I looked at Jeditable, but it has both more than I need and less than I need. Could be I just didn't look hard enough? – seth Nov 23 '10 at 23:49