-4

Can someone tell me how to disable the alert message in the following code?

jQuery(document).ready(function($){
    $(document).keydown(function(event) { 
        var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();        
        if (event.ctrlKey && (pressedKey == "v")) {
            alert('Sorry, This Functionality Has Been Disabled!'); 
            //disable key press processing
            return false; 
        }
    });
});

We have a chat site and we want to disable CTRL+V, for Firefox users it displays a popup message saying "Sorry, This Functionality Has Been Disabled!" and also it gives an option of "prevent this page from creating additional dialogues" and once you click that, then Users can use CTRL+v

Can we disable this message?

Liam
  • 27,717
  • 28
  • 128
  • 190
Softy
  • 17
  • 5
  • 2
    Possible duplicate of [How to disable Paste (Ctrl+V) with jQuery?](https://stackoverflow.com/questions/5510129/how-to-disable-paste-ctrlv-with-jquery) – Alexandre Elshobokshy Nov 02 '18 at 12:58
  • 4
    Why cant you just remove that line of code? – Jamiec Nov 02 '18 at 12:59
  • 1
    Do you realize that paste is more than ctrl+v? right click, edit/paste.... etc – epascarello Nov 02 '18 at 12:59
  • 1
    So you really want to actually disable copy/cut from the page, its not about the alert message. I think we should close this, as it doesnt really make sense. – JP Silvashy Nov 02 '18 at 13:04
  • I think OP want the popup to appear once and once it's appeared to stop. Though it's not clear. You probably want to look at [`.one()`](http://api.jquery.com/one/) – Liam Nov 02 '18 at 13:06
  • Do you only want to show the message the first time, and them prevent the paste from happening but not show the message again ? – Gabriele Petrioli Nov 02 '18 at 13:08

2 Answers2

1

Unfortunately you're not going to like this answer:

It's not really possible to totally prevent the user from accessing their clipboard, this cannot be completely controlled with JS as the functionality is sandboxed by the OS.

You can confuse the user by binding the key presses and doing other things, but you cannot actually prevent them from copy and pasting. You can also just not call alert altogether if you want to quietly ignore those key-presses.

JP Silvashy
  • 46,977
  • 48
  • 149
  • 227
0

My understanding is that you only want to show the message the first time and then keep blocking the action but not show the message.

So, you need a flag of whether you have shown the message

jQuery(document).ready(function($){
    var notifiedCtrlV = false;
    $(document).keydown(function(event) { 
        var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();        
        if (event.ctrlKey && (pressedKey == "v")) {
            event.preventDefault();
            if (!notifiedCtrlV) alert('Sorry, This Functionality Has Been Disabled!'); 
            notifiedCtrlV = true;
            //disable key press processing
            return false; 
        }
    });
});
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317