window.onload = function () { var body = document; //target any DOM element here
if (body.addEventListener) //(Mozilla) { body.addEventListener('dragenter', preventDrag, true); //precursor for drop event body.addEventListener('dragover', preventDrag, true); //precursor for drop event body.addEventListener('drop', preventDrag, true); } elseif (body.attachEvent) //(IE) { body.attachEvent('ondragenter', preventDrag); body.attachEvent('ondragover', preventDrag); body.attachEvent('ondrop', preventDrag); } } functionpreventDrag(event) { if (event.type == 'dragenter' || event.type == 'dragover' || //if drag over event -- allows for drop event to be captured, in case default for this is to not allow drag over target event.type == 'drop') //prevent text dragging -- IE and new Mozilla (like Firefox 3.5+) { if (event.target.className.trim() != "CodeMirror-line") { if (event.stopPropagation) //(Mozilla) { event.preventDefault(); event.stopPropagation(); //prevent drag operation from bubbling up and causing text to be modified on old Mozilla (before Firefox 3.5, which doesn't have drop event -- this avoids having to capture old dragdrop event) } returnfalse; //(IE) } } }