I am trying to update content of my page for printing. I want to have labels (spans) for each input element (textboxes, lists, etc.) to prevent text-cutoff when printing. I want
Whilst the above answers are excellent in answering the direct question the omit the following essential information:
Whilst the specific onbeforeprint
and onafterprint
events are not universally supported, there is functional support for the conceptual events - it IS possible to execute code before and after printing in most modern browsers.
See here for full details: http://tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/
Summary:
There exists a window.watchMedia API which allows you to hook into the transition events of CSS media queries, including the 'print'
media query. This allows you to hook into events occurring immediately before and immediately after you print a page.
This API is well supported by Chrome and Webkit, and the onbeforeprint
and onafterprint
events are supported in IE and Firefox giving you pretty good coverage.