How to stop user from printing webpages? using javascript or jquery

Viewed 29389

How can we stop users to print webpage using different methods?

  1. Disabling Right Click
  2. Disabling CtrlP combination of keys
  3. Right click -> print

Is there any way to stop printing webpages?

Can we handle these 3 events using javascript. Or we can say . if user will do any of these events. then i want to run my other code .Is this possible?

11 Answers

I tried this way of Chris

@media only print {
.container { display:none !important; }
}

it's work. But when I press F12 to open developer tool, find the line ".container { display:none !important; }" delete it, and then ctrl + p, every thing are show again on print windows. May be have no solution with 100% for prevent print webpage content.

It is not possible to stop web user to print your webpage. But yes you can set what to print after processed for print. My mean simply write code like below:

<!DOCTYPE html>
<html>
    <body onbeforeprint="abortPrint()">
        <p>This is my Content</p>

        <script>    
            function abortPrint()
            {
                alert("Printing is not allowed");
                document.write();
            }
        </script>
        
    </body>
</html>

This will simply print only blank page and your content will be not printed. But this only the way with the help of this you can stop user to print page.

Suggestion to answer is always appreciated.

Yes you can do it with JavaScript This script will disable everything from which a user can print like from the console or right clicking or ctrl+p And use it with css @media print{body{display:none;}} For a extra security.

Disable context menu on right-click,

$("body").on("contextmenu", function (e)  
   {  
      return false;  
   });  
}); 
//Or,
document.oncontextmenu = function() {
   return false;
}
//Disable right-click menu on a particular section on the page,
$(document).ready(function(){  
   $("#youDivSectionId").bind("contextmenu", function(e) {  
      return false;  
   });  
}); 
//Disable Cut, copy, paste,
$(document).ready(function(){
   $('body').bind('cut copy paste', function (e)
   {
      e.preventDefault();
   });
});
//Let's Block the same cut, copy, paste events with javascript codes,
$(document).ready(function(){  
$(document).keydown(function(event) {  
   //event.ctrlKey = check ctrl key is press or not  
   //event.which = check for F7  
   // event.which =check for v key  
   if (event.ctrlKey==true && (event.which == '118' || event.which == '86')) {  
      event.preventDefault();  
      }  
   });  
}); 
//Prevent browser Debugger console example,
$(document).keydown(function (event) {
// Prevent F12 -
if (event.keyCode == 123) {
   return false;
}
// Prevent Ctrl+a = disable select all
// Prevent Ctrl+u = disable view page source
// Prevent Ctrl+s = disable save
if (event.ctrlKey && (event.keyCode === 85 || event.keyCode === 83 || event.keyCode ===65 )) {
   return false;
}
// Prevent Ctrl+Shift+I = disabled debugger console using keys open
else if (event.ctrlKey && event.shiftKey && event.keyCode === 73)
{
   return false;
}
//print disable by ctrl+p
else if(event.ctrlKey && event.keyCode===80){
 return false;
  }
});

Using Javascript code you can block any event of browser.

Related