Is there a way to track if a user prints a web page?

∥☆過路亽.° 提交于 2019-12-17 16:27:14

问题


Is there a way to track if a user prints a web page?

Thanks, Graham


回答1:


@Tahir Akhtar had the idea of using a print stylesheet. According to the comments, that doesn't work because the stylesheet is always fetched , regardless of whether the browser currently is in print view or not.

How about taking this further: Use a print stylesheet and define a style in it that makes the browser fetch a certain resource when it's rendered. The resource would return a transparent image.

As for the choice of CSS property, background-image is out, because they are usually not rendered by the browser. My thought would be a list-style-image or a cursor.

I think list-style-image on an absolutely positioned, transparent <ul> might do the trick without visually altering anything (I would not dare to position the element outside the page because of possible browser optimizations cutting it away). Every access to that resource should come from printing a page, or a print preview.

I can't test it right now, maybe later. If anybody tries it, I'd be interested to hear whether it works.




回答2:


In Internet Explorer 5+, you can use the onafterprint event, to launch an AJAX request every time the page is printed. If you are using jQuery, you could do the following:

window.onafterprint = function()
{
    $.post("increment_print_counter.php");
};

Then maybe you can use some statistics to estimate the number of times it was printed from the other browsers!




回答3:


I don't know when the browsers would fetch the CSS when you specify them with media="print"

<link rel="stylesheet" type"text/css"
     href="print.css" media="print">

If they fetch it only when the user tries to print the document than you might try a server side trick to track it.

Give it a try.




回答4:


You can achieve this in IE only.

IE supports two client-side events that you can handle: onbeforeprint and onafterprint.

You can add an AJAX request in either event that will call a page server-side to, say, increment a PagePrinted counter in a database.




回答5:


For cross browser support, only if you offer to control printing, as in, adding a "display in printable format" button, which will at the minimum, show you the intent to print the page. However, you will not be able to stop a user from printing by conventional means or detect when the page is being printed.




回答6:


You could also add a big, obvious “Print” button to the page, that fires window.print, and does an AJAX request if onafterprint isn’t available.

It wouldn’t give you any stats about users who are just hitting Ctrl+P (and aren’t on IE), of course, but it gives you something (if you don’t mind sticking a “Print” button on the page).




回答7:


  1. Turn off print with CSS (no display on the body tag)
  2. Have a print button on page that pops open a new print only page. You can then tie that URL to the user (provided you have some info on them)



回答8:


Yes, for most browsers there are events that can be used to track print requests.

  • IE5+ supports window.onbeforeprint and window.onafterprint
  • Chrome9+ and Safari5.1+ support window.matchMedia()

See TJ VanToll's "Detecting Print Requests with JavaScript" blog post for more information.



来源:https://stackoverflow.com/questions/2148145/is-there-a-way-to-track-if-a-user-prints-a-web-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!