Add to collection
Expert User

Expert User

Verified

HTML page disable copy/paste - JavaScript

Disable the right-click (context menu) to prevent copy-and-paste. Disable the clipboard copy. CSS disable select and hide the highlighting of text.

Try Yourself in Code Editor    

Prevent the below things from a web page so that no one can copy or use your code.

  • Disable the right-click (context menu) to prevent copy-and-paste.
  • Disable the clipboard copy.
  • CSS disable select and hide the highlighting of text.
  • Prevent CTRL+S, CTRL+U, CTRL+P and CTRL+R
<script>
    // (A) PREVENT CONTEXT MENU FROM OPENING
    document.addEventListener("contextmenu", (evt) => {
      evt.preventDefault();
    }, false);

    // (B) PREVENT CLIPBOARD COPYING
    document.addEventListener("copy", (evt) => {
      // (B1) CHANGE THE COPIED TEXT IF YOU WANT
      evt.clipboardData.setData("text/plain", "Copying is not allowed on this webpage");

      // (B2) PREVENT THE DEFAULT COPY ACTION
      evt.preventDefault();
    }, false);
    
    
    $(document).bind('keydown', 'ctrl+u', function(e) {
    e.preventDefault();
    
    return false;
},false);
    
    
    </script>
<style>
    /* (C) NO SELECT + HIGHLIGHT COLOR */
    * { user-select: none; }
    *::selection { background: none; }
    *::-moz-selection { background: none; }
    </style>
<p>Disable the right-click (context menu) to prevent copy-and-paste.</p>
<p>Disable the clipboard copy.</p>
<p>CSS disable select and hide the highlighting of text.</p>

References and Credits

Comments 0

  • Sorry!! No comment posted yet. Become the first user to give comment.
Add a comment

Would you like to write for w3 sniff?

Become part of an amazing group of contributors and authors and start writing for us

Join Now
<script>
    // (A) PREVENT CONTEXT MENU FROM OPENING
    document.addEventListener("contextmenu", (evt) => {
      evt.preventDefault();
    }, false);

    // (B) PREVENT CLIPBOARD COPYING
    document.addEventListener("copy", (evt) => {
      // (B1) CHANGE THE COPIED TEXT IF YOU WANT
      evt.clipboardData.setData("text/plain", "Copying is not allowed on this webpage");

      // (B2) PREVENT THE DEFAULT COPY ACTION
      evt.preventDefault();
    }, false);
    
    
    $(document).bind('keydown', 'ctrl+u', function(e) {
    e.preventDefault();
    
    return false;
},false);
    
    
    </script>
<style>
    /* (C) NO SELECT + HIGHLIGHT COLOR */
    * { user-select: none; }
    *::selection { background: none; }
    *::-moz-selection { background: none; }
    </style>
<p>Disable the right-click (context menu) to prevent copy-and-paste.</p>
<p>Disable the clipboard copy.</p>
<p>CSS disable select and hide the highlighting of text.</p>
Feedback submitted successfully.