How To Disable Copy and Paste Using Javascript

In this article, we are going to learn how to Disable Copy and Paste Using Javascript. This is a simple source code to disable copy and paste of your content to your website. We are going to use the JavaScript script to do this function.

Creating HTML Source Code

This is our sample content in a page.
  1. <div style="height:480px; width:700px; margin:auto; padding:40px; border:2px solid #1982d1; border-radius:10px;">
  2. <h2 style="color:#1982d1;">DISABLE COPY AND PASTE</h2>
  3. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  4. You can write your own content here. You can even have your own blog.
  5. <br />
  6. <br />
  7. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  8. You can write your own content here. You can even have your own blog.
  9. <br />
  10. <br />
  11. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  12. You can write your own content here. You can even have your own blog.
  13. <br />
  14. <br />
  15. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  16. You can write your own content here. You can even have your own blog.
  17. <br />
  18. <br />
  19. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  20. You can write your own content here. You can even have your own blog.
  21. <br />
  22. <br />
  23. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  24. You can write your own content here. You can even have your own blog.
  25. <br />
  26. <br />
  27. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  28. You can write your own content here. You can even have your own blog.
  29. <br />
  30. <br />
  31. <b style="color:blue;">sourcecodester.com - Free Source Code</b> - Do you have source code, articles, tutorials, web links, and books to share?
  32. You can write your own content here. You can even have your own blog.
  33. </div>

This is the result of the code above:

Result

Creating JavaScript Script

In the code above, we are going to add the JavaScript script to disable copy and paste of the content of a page.
  1. <!-- Disable Copy and Paste-->
  2. <script language='JavaScript1.2'>
  3. function disableselect(e){
  4. return false
  5. }
  6. function reEnable(){
  7. return true
  8. }
  9. document.onselectstart=new Function (&quot;return false&quot;)
  10. if (window.sidebar){
  11. document.onmousedown=disableselect
  12. document.onclick=reEnable
  13. }
  14. </script>
  15.  
  16. <script>
  17. //disable mouse drag select start
  18. document.onselectstart = new Function('return false');
  19. function dMDown(e) { return false; }
  20. function dOClick() { return true; }
  21. document.onmousedown = dMDown;
  22. document.onclick = dOClick;
  23. $("#content_document").attr("unselectable", "on");
  24. //disable mouse drag select end
  25. //disable right click - context menu
  26. document.oncontextmenu = new Function("return false");
  27. //disable CTRL+A/CTRL+C through key board start
  28.  
  29. //use this function
  30. function disableSelectCopy(e) {
  31. // current pressed key
  32. var pressedKey = String.fromCharCode(e.keyCode).toLowerCase();
  33. if (e.ctrlKey && (pressedKey == "c" || pressedKey == "x" || pressedKey == "v" || pressedKey == "a")) {
  34. return false;
  35. }
  36. }
  37. document.onkeydown = disableSelectCopy;
  38.  
  39. //or use this function
  40. $(function () {
  41. $(document).keydown(function (objEvent) {
  42. if (objEvent.ctrlKey || objEvent.metaKey) {
  43. if (objEvent.keyCode == 65 || objEvent.keyCode == 97) {
  44. return false;
  45. }
  46. //}
  47. }
  48. });
  49. });
  50. </script>
So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment