JavaScript or JQuery

How to disable right-click | copy, paste in jquery, CSS, JavaScript?

There are several ways to disable the mouse right click. It prevents the copy and pasting in JavaScript.

Read more on Create Beautiful before and after image slider.

Use oncontextmenu to prevent copy text from web pages.

One way to disable the mouse right-click is to use the “oncontextmenu” event. This event occurs when the user right-clicks on an element. You can use the oncontextmenu event to display a custom menu or to cancel the default menu.

<body 
    oncontextmenu="return false"  
    onselectstart="return false" 
    ondragstart="return false" > 

    <!-- html code here --> 
</body>

See the live demo here.

Disable the mouse right click by using CSS.

You can use the “user-select” property to prevent the mouse right click.

When you double-click on text in a web browser, it is selected or highlighted. This can be avoided by using this property.

The “user-select” property is supported in Internet Explorer, Firefox, and Safari.

p {
    user-select:none;
}

The interactive demo is available here

Use jQuery to prevent content copying and to paste from your website

We’ll demonstrate how to use jQuery to avoid content theft from your website. You may quickly disable the mouse right click as well as cut, copy, and paste from web content by using jQuery.

Examples of jQuery

 //Disable full page
    $("body").on("contextmenu",function(e){
        return false;
    });

If you want to apply this feature to a specific element then you can use ID.

//Disable part of page
    $("#id").on("contextmenu",function(e){
        return false;
    });

You can also show messages on the screen

$(document).bind("contextmenu", function (e) {
  e.preventDefault();
  alert("Right Click is Disabled");
});

Disable Cut Copy and Paste

The following JavaScript code will prevent users from cut, copy, and pasting entire or specific elements of your website.

    //Disable for entire page
    $('body').bind('cut copy paste', function (e) {
        e.preventDefault();
    });
    
    //Disable specific element on page
    $('#id').bind('cut copy paste', function (e) {
        e.preventDefault();
    });

Disable ctrl+v, ctrl+x and ctrl+c.

// disable ctrl+x
$('textarea').on("cut", function(e) {
    $("#d2").text('cut. not allowed!');
    e.preventDefault();
});

// disable ctrl+c
$('textarea').on("copy", function(e) {
    $("#d2").text('copy. not allowed!');
    e.preventDefault();
});

// disable ctrl+v
$('textarea').on("paste", function(e) {
    $("#d2").text('paste. not allowed!');
    e.preventDefault();
});

Add comment