The onbeforeprint
property of the WindowEventHandlers
mixin is the EventHandler
for processing beforeprint
events for the current window. These events are raised before the print dialog window is opened.
The beforeprint
and afterprint
events allow pages to change their content before printing starts (perhaps to remove a banner, for example) and then revert those changes after printing has completed. In general, you should prefer the use of an @media print
CSS at-rule, but it may be necessary to use these events in some cases.
Syntax
window.addEventListener("beforeprint", function(event) { ... }); window.onbeforeprint = function(event) { ... };
Polyfill
Safari does not implement these events, but you can create an equivalent result to the beforeprint
event with window.matchMedia
('print')
.
var mediaQueryList = window.matchMedia('print'); mediaQueryList.addListener(function(mql) { if(mql.matches) { console.log('webkit equivalent of onbeforeprint'); } });
Specification
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'onbeforeprint' in that specification. |
Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
onbeforeprint | Chrome Full support 63 | Edge Full support 12 | Firefox Full support 6 | IE Full support Yes | Opera Full support 50 | Safari Full support 13 | WebView Android Full support 63 | Chrome Android Full support 63 | Firefox Android ? | Opera Android Full support 46 | Safari iOS Full support 13 | Samsung Internet Android Full support 8.0 |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown