Skip to main content

Disable selection of text using JavaScript

No replies
sanjeev's picture
Joined: 21 Feb 2011

I was browsing one website for some General Awareness Questions and I liked its content. So I tried to select the text so that I can copy paste and save it in my local hard drive.
But I was not allowed to select text from that website. I was amazed that this one is really coll feature and power of JavaScript.
I got this script in and thought to share it here with our viewers too. It will be useful , if you want to check users to copy paste your contents.

But as we all know JavaScript is client side language , so if someone views the source code then he/she can easily remove that script and can copy paste the text. But who cares, it's a nice feature and will help somehow to website owners.

Here is the working code:

  2. <html>
  3. <head>
  4. <title>Can't select text from Alien Coders
  5. </title>
  6. <script type="text/javascript">
  7. function IfTryToSelect(cont)
  8. {
  9. = "default"
  10. if (typeof cont.onselectstart!="undefined")
  11. {
  12. cont.onselectstart=function(){return false}
  13. }
  14. else if (typeof!="undefined")
  15. {
  17. }
  18. else
  19. {
  20. cont.onmousedown=function(){return false}
  21. }
  22. }
  23. </script>
  24. </head>
  26. <body onload="IfTryToSelect(document.body);">
  27. Hi Alien Coders,
  28. If you are using your mouse or keyboard to select this text. I bet you cannot select the text. <br />
  29. Because I have used JavaScript which can stop you to select and copy this text :D
  30. </body>
  31. </html>

Note: Always call the function inside a body tag or under the body tag else it will not work.
It worked in IE, Firefox and Chrome.

Follow us at :
Facebook | Twitter
########### Give me the right place to stand, I shall move the earth. #################

Post new comment

The content of this field is kept private and will not be shown publicly.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.