Is there a way to track if a user prints a web page?
Is 开发者_StackOverflow社区there a way to track if a user prints a web page?
Thanks, Graham
@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.
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!
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.
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.
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.
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).
- Turn off print with CSS (no display on the body tag)
- 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)
Yes, for most browsers there are events that can be used to track print requests.
- IE5+ supports
window.onbeforeprint
andwindow.onafterprint
- Chrome9+ and Safari5.1+ support
window.matchMedia()
See TJ VanToll's "Detecting Print Requests with JavaScript" blog post for more information.
精彩评论