Showing posts with label text. Show all posts
Showing posts with label text. Show all posts

Disable Text Selection

function doNothing(){ return false; }
function disableTextSelection(id) {
if (id=='all') var element=document;
  else var element=document.getElementById(id);
if (document.all)  // MSIE
   element.onselectstart = doNothing;
 else //Mozilla
   element.onmousedown = doNothing;
}
Use this script to disable the selection of text on a page. It's conducted by canceling the selection before it starts.
  • To disable text selection for entire document, assign 'all' as parameter, i.e.: disableTextSelection('all')
  • To attach the events to a specific element call the function with element id as parameter, i.e.: disableTextSelection('text1')
Text in this box is not selectable.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci suscipit lobortis ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan.

Get Text Selection

function selectedText() {
  if (document.getSelection) {
    var str = document.getSelection();
    if (window.RegExp) {
      var regstr = unescape("%20%20%20%20%20");
      var regexp = new RegExp(regstr, "g");
      str = str.replace(regexp, "");
    }
  } else if (document.selection && document.selection.createRange) {
    var range = document.selection.createRange();
    var str = range.text;
  } else {
    var str = "";
  }
  return str;
}
Working example: Try select some text anywhere in this page and press