Created
March 1, 2013 19:18
-
-
Save jacksonfdam/5067028 to your computer and use it in GitHub Desktop.
disable text drag and drop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
#ondragstart is for IE, onmousedown is for firefox | |
ondragstart="return false" onmousedown="return false" | |
--> | |
<body ondragstart="return false" draggable="false" | |
ondragenter="event.dataTransfer.dropEffect='none'; event.stopPropagation(); event.preventDefault();" | |
ondragover="event.dataTransfer.dropEffect='none';event.stopPropagation(); event.preventDefault();" | |
ondrop="event.dataTransfer.dropEffect='none';event.stopPropagation(); event.preventDefault();" | |
> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function preventDrag(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.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) | |
} | |
return false; //(IE) | |
} | |
} | |
//attach event listeners after page has loaded | |
window.onload=function() | |
{ | |
var myTextInput = document.getElementById('textInput'); //target any DOM element here | |
if(myTextInput.addEventListener) //(Mozilla) | |
{ | |
myTextInput.addEventListener('dragenter', handleEvents, true); //precursor for drop event | |
myTextInput.addEventListener('dragover', handleEvents, true); //precursor for drop event | |
myTextInput.addEventListener('drop', preventDrag, true); | |
} | |
else if (myTextInput.attachEvent) //(IE) | |
{ | |
myTextInput.attachEvent('ondragenter', preventDrag); | |
myTextInput.attachEvent('ondragover', preventDrag); | |
myTextInput.attachEvent('ondrop', preventDrag); | |
} | |
} | |
//OU | |
function pauseEvent(e){ | |
if(e.stopPropagation) e.stopPropagation(); | |
if(e.preventDefault) e.preventDefault(); | |
e.cancelBubble=true; | |
e.returnValue=false; | |
return false; | |
} | |
// OU | |
document.onselectstart = function() | |
{ | |
window.getSelection().removeAllRanges(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment