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

后端 未结 9 672
谎友^
谎友^ 2020-12-03 12:34

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

  1. Disabling Right Click
  2. Disabling CtrlP combination of keys
  3. <
相关标签:
9条回答
  • 2020-12-03 13:25

    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.

    0 讨论(0)
  • 2020-12-03 13:26

    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

    0 讨论(0)
  • 2020-12-03 13:29

    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.

    0 讨论(0)
提交回复
热议问题