如何阻止用户打印网页?使用 javascript 或 jquery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6647392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop user from printing webpages? using javascript or jquery
提问by Gourav khanna
How can we stop users to print webpage using different methods?
我们如何阻止用户使用不同的方法打印网页?
- Disabling Right Click
- Disabling CtrlPcombination of keys
- Right click -> print
- 禁用右键单击
- 禁用CtrlP组合键
- 右键->打印
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?
我们可以使用 javascript 处理这 3 个事件吗?或者我们可以说。如果用户将执行这些事件中的任何一个。然后我想运行我的其他代码。这可能吗?
回答by Kumar
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.
用户机器上的网页不受您的控制。用户可以做他或她想做的任何事情,但是您可以提供 PDF 并禁用打印该 PDF。尽管如此,如果我必须打印一些东西,我会找到一种打印它的方法。
回答by James Allardice
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 guaranteedto work, and it's easy to get around if you have even a vague idea what you're doing.
但这并不能保证有效,如果您甚至对自己在做什么有一个模糊的概念,也很容易绕过。
回答by Chris
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
.
我知道这个问题很久以前就有人问过并回答过,但我遇到了它,觉得我应该投入 2 美分。我有一个包含一些版权信息的程序,我们希望最终用户不是能够打印这些信息,所以我所做的是创建一个单独的 div 给 class .printable
。
I added these lines to the CSS sheet
我将这些行添加到 CSS 表中
.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.
随着主流浏览器中新的 CSS 引擎的改进,即使他们执行右键单击 + 打印,它也会显示.printable
框而不是内容。
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.
然而,有人指出,如果用户真的想打印信息,没有办法阻止用户使用屏幕捕获软件打印信息。我们认为,通过阻止打印,我们将阻止大约 99% 的人可能会以其他方式打印信息。
So that's my 2 cents... hope it helps someone
所以这是我的 2 美分...希望它可以帮助某人
回答by deceze
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.
一旦用户将您网站的数据下载到他的计算机上,您就无法再控制它。如果您不想让用户随心所欲地使用它,请不要将其放在公共网络上。
回答by Gedrox
Add such block to your CSS
将此类块添加到您的 CSS
@media print {
body {
display: none;
}
}
This should hide all content when printing.
这应该在打印时隐藏所有内容。
回答by Dimitar Petrov
I wouldn't fight the users expectations. Don't change the default behaviour.
我不会违背用户的期望。不要更改默认行为。
回答by Brian Scott
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.
即使是 Aviary 等浏览器插件也会抓取浏览器的屏幕截图并将其复制到内存中,因此您无法阻止这种情况发生。然后用户可以从 photoshop 或paint .net 等打印。
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.
我唯一建议你尝试的是你可以包含一个仅 print.css 的样式表。在该样式表中,您可能希望尝试将任何敏感内容设置为 display:none。然而,即使这样也不能保证,因为用户可以简单地禁用样式表。
回答by Kangkan
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.
如果您希望向用户显示某些信息,并且不希望用户能够打印,则可以使用 PDF 之类的内容并保护它以防止打印。
What is your kind of usage?
你的用途是什么?