How to Disable the CTRL+P using javascript or Jquery?

后端 未结 11 1038
一生所求
一生所求 2020-12-31 06:17

Here I tried to disable the Ctrl+P but it doesn\'t get me alert and also it shows the print options

jQuery(document).bind(\"keyup keydo         


        
相关标签:
11条回答
  • 2020-12-31 06:32

    This Actually worked for me in chrome. I was pretty suprised.

    jQuery(document).bind("keyup keydown", function(e){
        if(e.ctrlKey && e.keyCode == 80){
             Print(); e.preventDefault();
        }
    });
    

    Where Print is a function I wrote that calls window.print(); It also works as a pure blocker if you disable Print();

    As noted here: https://stackoverflow.com/a/20121038/2102085

    window.print() will pause so you can add an onPrintFinish or onPrintBegin like this

    function Print(){
        onPrintBegin
        window.print();
        onPrintFinish(); 
    }
    

    (Again this is just chrome, but Peter has a downvoted solution below that claims the keycodes are different for ff and ie)

    0 讨论(0)
  • 2020-12-31 06:34

    Try this

        //hide body on Ctrl + P
        jQuery(document).bind("keyup keydown", function (e) {
            if (e.ctrlKey && e.keyCode == 80) {
                $("body").hide();
                return false;
            }
        });
    
    0 讨论(0)
  • 2020-12-31 06:36

    Your code works in the jsfiddle example? What browser are you using? Itested it with the latest chrome and it worked fine.

    You can also add:

    e.preventDefault();
    
    0 讨论(0)
  • 2020-12-31 06:36

    If you want to disable printing of your webpage you're wasting your time: it can't be done. Even if you work out how to capture CTRL-P users can still use the browsers menu bar to find the print command, or they can take a screen shot of the browser.

    Stop trying to control the user, put your energy into making your site / app more useful, not less useful.

    edit 2016: in the 3 years this has been up it has gathered 3 downvotes. I'm still not deleting it. I think it is important to tell fellow developers when they are given impossible tasks, or tasks that make no sense.

    edit 2018: still think it's important that people that have this question read this answer.

    0 讨论(0)
  • 2020-12-31 06:38

    had a journy finding this, should be canceled on the keydown event

    document.addEventListener('keydown',function(e){
       e.preventDefault();
       return false;
    });
    

    further simplified to :

    document.onkeydown = function(e){
       e.preventDefault();
    }
    

    given you have only one keydown event

    0 讨论(0)
提交回复
热议问题