Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
163
rated 0 times [  168] [ 5]  / answers: 1 / hits: 55947  / 11 Years ago, tue, september 17, 2013, 12:00:00

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


More From » jquery

 Answers
25

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)


[#75654] Sunday, September 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rohan

Total Points: 403
Total Questions: 93
Total Answers: 105

Location: Trinidad and Tobago
Member since Mon, Jul 13, 2020
4 Years ago
rohan questions
Tue, Sep 8, 20, 00:00, 4 Years ago
Fri, Aug 21, 20, 00:00, 4 Years ago
Mon, Aug 3, 20, 00:00, 4 Years ago
Thu, Jul 2, 20, 00:00, 4 Years ago
;