Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 keydown", function(e){
    if(e.ctrlKey && e.keyCode == 80){
        alert('fine');
        return false;
    }
});

http://jsfiddle.net/qaapD/10/

I am not sure how can I disable the Ctrl+P combination itself using jQuery or JavaScript.

Thanks

like image 369
Vignesh Pichamani Avatar asked Sep 17 '13 07:09

Vignesh Pichamani


People also ask

How do I disable Ctrl P in Windows 10?

Steps to disable or enable Ctrl key shortcuts in CMD on Windows 10: Step 1: Open Command Prompt. Step 2: Right-tap the Title bar and choose Properties. Step 3: In Options, deselect or select Enable Ctrl key shortcuts and hit OK.

How do I turn off the Print option on a website?

this can be done by simple css code. CSS code means Cascading Style sheet. Jut add a print media query in css file. if you will add this CSS code in head section of HTML tag then visitor can not print webpage.


1 Answers

You can't prevent the user from printing, but you can hide everything when the user prints the document by using simple CSS:

<style type="text/css" media="print">
    * { display: none; }
</style>

Updated fiddle.

If you would like to show the visitor a custom message when he/she try to print rather then just a blank page, it's possible with client side code but first wrap all your existing contents like this:

<div id="AllContent">
    <!-- all content here -->
</div>

And add such a container with the custom message:

<div class="PrintMessage">You are not authorized to print this document</div>

Now get rid of the <style type="text/css" media="print"> block and the code would be:

if ('matchMedia' in window) {
    // Chrome, Firefox, and IE 10 support mediaMatch listeners
    window.matchMedia('print').addListener(function(media) {
        if (media.matches) {
            beforePrint();
        } else {
            // Fires immediately, so wait for the first mouse movement
            $(document).one('mouseover', afterPrint);
        }
    });
} else {
    // IE and Firefox fire before/after events
    $(window).on('beforeprint', beforePrint);
    $(window).on('afterprint', afterPrint);
}

function beforePrint() {
    $("#AllContent").hide();
    $(".PrintMessage").show();
}

function afterPrint() {
    $(".PrintMessage").hide();
    $("#AllContent").show();
}

Code is adopted from this excellent answer.

Updated fiddle. (showing message when printing)

like image 125
Shadow Wizard Hates Omicron Avatar answered Sep 18 '22 11:09

Shadow Wizard Hates Omicron