Quick History

A couple years ago, one of my clients recently had the need to keep track of pages that were printed by the staff for back-office type operations. They were using Internet Explorer exclusively, so I felt comfortable using IE’s proprietary onafterprint event.

Within this handler of this event, I was using AJAX to hit a server-side script that kept track of the number of times a given page was printed. To be accurate, this event was also fired after a page was previewed for printing.

I thought that kinda strange, since it was a JavaScript event fired by the browser, it should have had the fined-grained control to tell us when the page actually printed (as opposed to simply being previewed).

Anyway, this solution worked perfectly, until my client decided to explore other browsers. One day I get the email – “why doesn’t our print tracking work anymore?”.

Options for modern browsers

After digging into it for a few minutes, I came to the conclusion that they were no longer using Internet Explorer exclusively.

After a quick Google search, I found that the window.matchMedia() function would quickly solve this issue. The only problem is, it isn’t fully supported by legacy browsers.

I started composing an email to my client to the effect of, “any chance you guys can start using the latest Chrome exclusively?”.  Then, it hit me, instead of putting the workload on my client, there was a simple solution that has been supported by all browsers since the dawn of time (well, close enough).

CSS Background Images

I could drop a background image style inside a print-only CSS block. But, instead of supplying a background image, I’d use a server-side script and supply any necessary tracking options in the query string.

<style type="text/css">
  @media print { 
    .order-date { 
      background-image: url(/updatePrinted.php?page_id=x); 
    } 
  }
</style>

The browser certainly doesn’t like the content-type response, but it does the job. I suppose I could offer up a content-type: image/gif header and a very small image as the response, but it’s not really necessary to get the job done.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *