Unselectable Text

There are many methods and options to make text unselectable on a website, but not all of them work on the major browsers. Below please find different source code to help you achieve this.

If you combine all the below source codes together in the same html/asp/aspx form, you’ll get all their functionality on the same page, thus no one can select any text from your website from the following browsers:

  • Internet Explorer 7
  • Firefox 3.0.4
  • Google Chrome 0.4.x
  • Opera 9.62

Source Code 1 – Unselectable

var Unselectable = {

enable : function(e) {
var e = e ? e : window.event;

if (e.button != 1) {
if (e.target) {
var targer = e.target;
} else if (e.srcElement) {
var targer = e.srcElement;
}

var targetTag = targer.tagName.toLowerCase();
if ((targetTag != “input”) && (targetTag != “textarea”)) {
return false;
}
}
},

disable : function () {
return true;
}

}

if (typeof(document.onselectstart) != “undefined”) {
document.onselectstart = Unselectable.enable;
} else {
document.onmousedown = Unselectable.enable;
document.onmouseup = Unselectable.disable;
}

You need to insert this in the <head> section of the html form in the following form:

<

 

script type=”text/javascript”>

 

….enter source code here…

 

</script>

This source code is from webtoolkit.info. This code will disable text selection by mouse dragging. This was tested and worked on the following browsers:

  • Internet Explorer 7
  • Firefox 3.0.4
  • Google Chrome 0.4.x
  • Opera 9.62

 This code will also disable text selection by the ctrl+a keys combination but only works in IE7.

Source Code 2: OnLoad

This is very simple and it disables text selection by mouse dragging.

  1. <script type=”text/javascript”>
  2. window.onload =
  3. function()

  4. {
  5.      document.onselectstart =
  • function() {return false;} // ie
  •      //document.onmousedown = function() {return false;} // mozilla
  •      document.style.MozUserSelect =
  • “none”;

  •      document.oncontextmenu =
  • function() {return false;}

  • }