theDeSilva.com Andrew de Silva

Disable selection on browser using Javascript

March 10th, 2010 · No Comments · Javascript

Here’s a quick way to disable selection on browsers using Javascript. Helpful if you don’t want somebody to select a bunch of text and copy and paste.

1
2
3
4
5
6
7
8
9
10
11
12
13
window.onload = function() {
	disableSelection(document.body)
}
 
function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE 
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox 
	target.style.MozUserSelect="none"
else //All other ie: Opera
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}

Tags: ··