Friday, January 10, 2014

Javascript: How to disable text selection on a webpage

I'm usually against this, but sometimes the client insists on this feature. Just put the following code before the closing tag of the head...

 <script type="text/JavaScript">  
   function killCopy(e){  
     return false  
   }  
   function reEnable(){  
     return true  
   }  
   document.onselectstart=new Function ("return false")  
   if (window.sidebar){  
     document.onmousedown=killCopy  
     document.onclick=reEnable  
   }  
 </script>  

Of course, at the end we didn't use this code as it also disabled the searh form input field...