How to trigger javascript on print event?

后端 未结 4 657
心在旅途
心在旅途 2020-12-16 10:03

Is it possible to trigger a javascript event when a user prints a page? I would like to remove a dependancy on a javascript library, when a user opts to print a page, as the

4条回答
  •  离开以前
    2020-12-16 10:29

    It can be done by overwriting, e.g., window.onbeforeprint.

    Using Chrome, I found that the more arcane window.matchMedia("print").addListener(function() {alert("Print Dialog open.")}) also works.

    This debatable feature can be used in order to deter users from printing a page.

    I've encountered it the first time on Scribd. There, if you open the print dialog using the menu command, the page content will be greyed out and a warning pop-over message appears explaining why you can not print the page. (Note for complete analysis: on that page, control-p is also overriden so you can not use it to open the print dialog at all. Additionally, there is a @media CSS for printer output that hides the content; all tested on Firefox).

提交回复
热议问题