Do modern browsers support onbeforeprint / onafterprint HTML events?

后端 未结 3 565
既然无缘
既然无缘 2021-01-24 01:58

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

3条回答
  •  Happy的楠姐
    2021-01-24 02:33

    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.

提交回复
热议问题