How to detect Ctrl+V, Ctrl+C using JavaScript?

前端 未结 17 2060
走了就别回头了
走了就别回头了 2020-11-22 13:47

How to detect ctrl+v, ctrl+c using Javascript?

I need to restrict pasting in my textareas, end user should not copy and p

17条回答
  •  日久生厌
    2020-11-22 14:28

    There is some ways to prevent it.

    However the user will be always able to turn the javascript off or just look on the source code of the page.

    Some examples (require jQuery)

    /**
    * Stop every keystroke with ctrl key pressed
    */
    $(".textbox").keydown(function(){
        if (event.ctrlKey==true) {
            return false;
        }
    });
    
    /**
    * Clear all data of clipboard on focus
    */
    $(".textbox").focus(function(){
        if ( window.clipboardData ) {
            window.clipboardData.setData('text','');
        }
    });
    
    /**
    * Block the paste event
    */
    $(".textbox").bind('paste',function(e){return false;});
    

    Edit: How Tim Down said, this functions are all browser dependents.

提交回复
热议问题