{"id":127283,"date":"2022-08-14T15:01:12","date_gmt":"2022-08-14T15:01:12","guid":{"rendered":"https:\/\/phppot.com\/?p=18911"},"modified":"2022-08-14T15:01:12","modified_gmt":"2022-08-14T15:01:12","slug":"disable-mouse-right-click-cut-copy-paste-with-javascript-or-jquery","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/08\/14\/disable-mouse-right-click-cut-copy-paste-with-javascript-or-jquery\/","title":{"rendered":"Disable mouse right click, cut, copy, paste with JavaScript or jQuery"},"content":{"rendered":"<div class=\"modified-on\" readability=\"7.1111111111111\"> by <a href=\"https:\/\/phppot.com\/about\/\">Vincy<\/a>. Last modified on August 14th, 2022.<\/div>\n<p>Before learning how to disable the cut, copy, paste and right-click event in JavaScript we should know first Why.<\/p>\n<p>Why we need this is, for <a href=\"https:\/\/phppot.com\/css\/how-to-make-online-photo-editing-effects-like-blur-image-sepia-vintage\/\">copying and manipulating content<\/a> displayed on the client. It lets the user struggle to do the following for example.<\/p>\n<ol>\n<li>Take the copyrighted site content by copy and paste.<\/li>\n<li><a href=\"https:\/\/phppot.com\/php\/extract-images-from-url-in-excel-with-php-using-phpspreadsheet\/\">Save media files by right-clicking<\/a> and saving assets.<\/li>\n<\/ol>\n<p>We implement this disabling by using the below steps. The first two of them are mandatory and the 3rd one is optional.<\/p>\n<ol>\n<li>Listen to the event type cut, copy, paste and right-click.<\/li>\n<li>Prevent the default behavior with the reference of the <em>event<\/em> object.<\/li>\n<li>Acknowledge users by showing alert messages [optional step].<\/li>\n<\/ol>\n<p><a class=\"demo\" href=\"https:\/\/phppot.com\/demo\/disable-mouse-right-click-cut-copy-paste\/\">View Demo<\/a><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-large wp-image-18941\" src=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/disable-cut-copy-paste-right-click-550x281.jpg\" alt=\"disable cut copy paste right click\" width=\"550\" height=\"281\" srcset=\"https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/disable-cut-copy-paste-right-click-550x281.jpg 550w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/disable-cut-copy-paste-right-click-300x153.jpg 300w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/disable-cut-copy-paste-right-click-768x392.jpg 768w, https:\/\/phppot.com\/wp-content\/uploads\/2022\/08\/disable-cut-copy-paste-right-click.jpg 800w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\"><\/p>\n<h2>Example 1 \u2013 Disabling cut, copy, paste and mouse right-click events<\/h2>\n<p>This JavaScript disables the cut, copy, paste and right-click on a textarea content.<\/p>\n<p>It defines a callback invoked on document ready event. It uses jQuery.<\/p>\n<p>In this callback, it listens to the cut, copy, paste and the mouse right-click action requests.<\/p>\n<p>When these requests are raised this script prevents the default behavior. Hence, it stops the expected action based on the cut, copy, and other requests mentioned above.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">$(document).ready(function() { $('textarea').on(\"cut\", function(e) { e.preventDefault(); alertUser('Cut'); }); $('textarea').on(\"copy\", function(e) { e.preventDefault(); alertUser('Copy'); }); $('textarea').on(\"paste\", function(e) { e.preventDefault(); alertUser('Paste'); }); $(\"textarea\").on(\"contextmenu\", function(e) { e.preventDefault(); alertUser('Mouse right click'); });\n}); function alertUser(message) { $(\"#phppot-message\").text(message + ' is disabled.'); $(\"#phppot-message\").show();\n}\n<\/code><\/pre>\n<h3>HTML with Textarea and alert box<\/h3>\n<p>This HTML has the textarea. The JavaScript targets this textarea content to disable the cut, copy and more events.<\/p>\n<p>Once disabled the default behavior, the UI should notify the user by <a href=\"https:\/\/phppot.com\/php\/add-edit-comments-using-jquery-dialogify\/\">displaying a message<\/a>.<\/p>\n<p>So, it contains an HTML alert box to show a red ribbon with a related error message to let the user understand.<\/p>\n<p>It includes the <a href=\"https:\/\/releases.jquery.com\/\" target=\"_blank\" rel=\"noopener\">jQuery library with a CDN URL<\/a>. Insert the above script next to the jQuery include then run the example.<\/p>\n<p>The downloadable at the end of this article contains the complete working example.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Disable mouse right click, cut, copy, paste with JavaScript or jQuery&lt;\/title&gt;\n&lt;meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" \/&gt;\n&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"css\/style.css\" \/&gt;\n&lt;link rel=\"stylesheet\" type=\"text\/css\" href=\"css\/form.css\" \/&gt;\n&lt;style&gt;\n#phppot-message { display: none;\n}\n&lt;\/style&gt;\n&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.6.0.min.js\" integrity=\"sha256-\/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej\/m4=\" crossorigin=\"anonymous\"&gt;&lt;\/script&gt; &lt;\/head&gt; &lt;body&gt; &lt;div class=\"phppot-container\"&gt; &lt;h2&gt;Disable mouse right click, cut, copy, paste&lt;\/h2&gt; &lt;textarea name=\"notes\" class=\"full-width\" rows=\"5\"&gt;&lt;\/textarea&gt; &lt;div id=\"phppot-message\" class=\"error\"&gt;&lt;\/div&gt; &lt;\/div&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<h2>Example 2 \u2013 Thin version using jQuery<\/h2>\n<p>This example is a thin version of the above. It also uses the jQuery library to disable the cut, copy paste and right-click events.<\/p>\n<p>This binds all the events to be disabled. It has a single-line code instead of creating exclusive listeners for each event.<\/p>\n<p>This method has a slight disadvantage. That is, it shows a generic message while stopping any one of the cut, copy, paste and right-click events.<\/p>\n<p>In the first method, the <a href=\"https:\/\/phppot.com\/jquery\/jquery-show-hide\/\">acknowledgment message<\/a> is too specific. That message gives more clarity on what is triggered and what is disabled and prevented.<\/p>\n<p>But, the advantage of the below example is that it is too thin to have it as a client-side feature in our application.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">$(document).ready(function() { $('textarea').bind('cut copy paste contextmenu', function(e) { e.preventDefault(); $(\"#phppot-message\").text('The Cut copy paste and the mouse right-click are disabled.'); $(\"#phppot-message\").show(); });\n});\n<\/code><\/pre>\n<h2>Example 3 \u2013 For JavaScript lovers<\/h2>\n<p>Do you want a pure JavaScript solution for this example? The below script achieves this to disable cut, copy, paste and right-click.<\/p>\n<p>It has no jQuery or any other client-side framework or libraries. I believe that the frameworks are for achieving a volume of effects, utils and more.<\/p>\n<p>If the application is going to have thin requirements, then no need for any framework.<\/p>\n<h3>HTML with onCut, onCopy, onPaste and onContextMenu attributes<\/h3>\n<p>The Textarea field in the below HTML has the following callback attributes.<\/p>\n<ul>\n<li>onCut<\/li>\n<li>OnCopy<\/li>\n<li>onPaste<\/li>\n<li>onContextMenu<\/li>\n<\/ul>\n<p>In these event occurrences, it calls the&nbsp;<em>disableAction()<\/em>. It sends the event object and a string to specify the event occurred.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-html\">&lt;textarea name=\"notes\" class=\"full-width\" rows=\"5\" onCut=\"disableAction(event, 'Cut');\" onCopy=\"disableAction(event, 'Copy');\" onpaste=\"disableAction(event, 'Paste');\" oncontextmenu=\"disableAction(event, 'Right click');\"&gt;&lt;\/textarea&gt;\n&lt;div id=\"phppot-message\" class=\"error\"&gt;&lt;\/div&gt;\n<\/code><\/pre>\n<h3>JavaScript function called on cut, copy, paste and on right click<\/h3>\n<p>In the JavaScript code, it reads the event type and disables it.<\/p>\n<p>The event.preventDefault() is the common step in all the <a href=\"https:\/\/phppot.com\/jquery\/enable-disable-submit-button-based-on-validation\/\">examples to disable<\/a> the cut, copy, paste and right click.<\/p>\n<pre class=\"prettyprint\"><code class=\"language-javascript\">function disableAction(event, action) { event.preventDefault(); document.getElementById(\"phppot-message\").innerText = action + ' is disabled.'; document.getElementById(\"phppot-message\").style.display = 'block';\n}\n<\/code><\/pre>\n<h2>Disclaimer<\/h2>\n<p>The keyboard or mouse event callbacks like <em>onMouseDown()<\/em> or <em>onKeyDown<\/em> lags in performance. Also, it has its disadvantages. Some of them are listed below.<\/p>\n<ol>\n<li>The keys can be configured and customized. So, event handling with respect to the keycode is not dependable.<\/li>\n<li>On clicking the mouse right, it displays the menu before the <em>onMouseDown()<\/em> gets and checks the key code. So, disabling mouse-right-click is failed by using <em>onMouseDown()<\/em> callback.<\/li>\n<li>Above all, an user can easily disable JavaScript in his browser and use the website.<\/li>\n<\/ol>\n<p><a class=\"demo\" href=\"https:\/\/phppot.com\/demo\/disable-mouse-right-click-cut-copy-paste\/\">View Demo<\/a><a class=\"download\" href=\"https:\/\/phppot.com\/downloads\/disable-mouse-right-click-cut-copy-paste.zip\">Download<\/a><\/p>\n<p> <!-- #comments --> <\/p>\n<div class=\"related-articles\">\n<h2>Popular Articles<\/h2>\n<\/p><\/div>\n<p> <a href=\"https:\/\/phppot.com\/javascript\/disable-mouse-right-click-cut-copy-paste\/#top\" class=\"top\">\u2191 Back to Top<\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>by Vincy. Last modified on August 14th, 2022. Before learning how to disable the cut, copy, paste and right-click event in JavaScript we should know first Why. Why we need this is, for copying and manipulating content displayed on the client. It lets the user struggle to do the following for example. Take the copyrighted [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":127284,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"class_list":["post-127283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-updates"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=127283"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/127283\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media\/127284"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=127283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=127283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=127283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}