Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
////////////////////////////////////////////////////////////////////////////
// UTILITIES - Section contains general-purpose utilties //
// ========= //
////////////////////////////////////////////////////////////////////////////
utilities : {
savedValueOf : new Object(), // savedValueOf will hold "original" values that we override/restore as needed
disableTextSelection : function() {
switch (true) {
case ( typeof document.onselectstart!="undefined" ) : // IE
this.savedValueOf["onselectstart"] = document.onselectstart;
document.onselectstart=function() { return false; };
break;
case ( typeof document.body.style.MozUserSelect != "undefined" ) : // Firefox
this.savedValueOf["-moz-user-select"] = document.body.style.MozUserSelect || "text";
document.body.style.MozUserSelect="none";
break;
case ( document.body.style["-khtml-user-select"] != "undefined" ) : // Safari
this.savedValueOf["-khtml-user-select"] = document.body.style["-khtml-user-select"];
document.body.style["-khtml-user-select"] = 'none';
break;
}
},
enableTextSelection : function() {
switch (true) {
case ( typeof document.onselectstart!="undefined" ) : // IE
document.onselectstart = this.savedValueOf["onselectstart"]
break;
case (typeof document.body.style.MozUserSelect != "undefined") : // Firefox
document.body.style.MozUserSelect = this.savedValueOf["-moz-user-select"]
break;
case ( document.body.style["-khtml-user-select"]!="undefined" ) : // Safari
document.body.style["-khtml-user-select"] = this.savedValueOf["-khtml-user-select"];
break;
}
}
}
Owner

webdood commented May 18, 2010

Unified Text Selection Disable/Enable Routine

If you're doing any kind of drag and drop operation in Javascript/DHTML, you will need to temporarily disable and re-enable text selection in your document while the drag operation is going on.

I wrote this block of code today and it was such a tedious hassle, I thought it worth blogging so others wouldn't have to endure my pain :{.

This works in all browsers except PC Opera.

It DOES work in Mac OSX Safari, Firefox, Chrome and on PC Internet Explorer, Safari, Firefox and Chrome.

Shannon Norrell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment