How to stop user from printing webpages? using javascript or jquery
How can we stop users to print webpage using different methods?
- Disabling Right Click
- Disabling CtrlP combination of keys
- Right click -> print
Is there any way to stop printing webpages?
Can w开发者_开发知识库e 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?
As has already been noted, you cannot do this. However, one commonly used trick is to try and hide all the page content if it's being printed:
<style type="text/css" media="print">
body { visibility: hidden; display: none }
</style>
But this is not guaranteed to work, and it's easy to get around if you have even a vague idea what you're doing.
A webpage on a user's machine is out of your control. A user can do whatever he or she wants to do, however you can serve a PDF and disable printing that PDF. Still, if I have to print some thing I will find a way to print it.
I know this question was asked and answered a long time ago, but I came across it and felt that I should throw my 2 cents in. I have a program that has some copyright information in it, we would prefer that the end user not be able to print this information, so what I did was create a separate div that I gave the class .printable
.
I added these lines to the CSS sheet
.printable { display:none; }
@media only print {
.container { display:none !important; } <-- This is the wrapper container for all of the site data
.printable { display:block !important; } <-- This is my added printable container with a message about not printing the page
}
With the improvements in the new CSS engines in the major browsers, even if they do a right click + print, it will display the .printable
box instead of the content.
However, it has been pointed out, that there is no way to prevent a user from using a piece of screen capture software to print the information if they really wanted to. We felt that by discouraging the printing, we will stop about 99% of the people who might have otherwise printed the information.
So that's my 2 cents... hope it helps someone
You can't. Stop wasting your time.
As soon as the user has downloaded the data of your website to his computer, you have no control over it anymore. If you don't want the user to do with it what he likes, don't put it on the public web.
Add such block to your CSS
@media print {
body {
display: none;
}
}
This should hide all content when printing.
There's just no reliable way to do this. You can intercept certain key presses etc and cancel them using script but when the user has script disabled then there's no prevention. Also, the print functionality is built into the actual browser itself, you can't actually prevent this.
Even browser plugins such as Aviary will grab a screenshot of the browser and copy it into memory so you wouldn't be able to prevent this happening. The user could then just print from photoshop or paint .net etc.
The only thing I would suggest you try is that you can include a print.css only stylesheet. Within that stylesheet you may wish to try setting any sensitive content as display:none. However, even this is not guaranteed as the user could simply disable stylesheets.
I wouldn't fight the users expectations. Don't change the default behaviour.
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.
If you render something onto the browser of the user, it becomes in the control of the user. You can not resist the user from printing.
If you wish some information to be shown to the user, and you do not want the user to be able to print, you can use something like a PDF and securing it against printing.
What is your kind of usage?
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.
精彩评论