spacer.png, 0 kB
spacer.png, 0 kB
Home arrow Javascript arrow Javascript: disable text selection
Javascript: disable text selection PDF Print E-mail
Written by Administrator   
venerd́, 22 agosto 2008

The way to disable (and then copy and paste) a page web content could be an useful way to protect your information even if could be also annoying for the final user surfing the page or even against the free spirit of sharing web data.
The function here below shows how to protect a portion of text (or even the entire document) from user selection.

<script language="Javascript">

function disableSelection (element) {
  if (typeof element.onselectstart!="undefined")
    element.onselectstart=function(){return false}
  else if (typeof element.style.MozUserSelect!="undefined")
    element.style.MozUserSelect="none";
  else
    element.onmousedown=function(){return false}
  element.style.cursor = "default"
}

</script>

You can call this function then with the element you want to protect as parameter (the whole document such as line 2 here below or just the "Div1" box, in the following line).

<script language="Javascript">
disableSelection(document.body);
disableSelection(document.getElementById("Div1"));
</script>



Here's an example box
In this box the possibility to select and copy text and images has been disabled with the above script.
Last Updated ( sabato, 23 agosto 2008 )
 
< Prev   Next >
spacer.png, 0 kB
spacer.png, 0 kB
spacer.png, 0 kB