English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The beforeprint event is triggered when the associated document is about to be printed or previewed for printing.
Execute JavaScript when the page is to be printed:
<!DOCTYPE html> <html> <head> <title>HTML onbeforeprint event property usage (basic tutorial website oldtoolbag.com)</title> <body onbeforeprint="myFunction()"> <h1>Try to print this document</h1> <p><b>Tip:</b> Keyboard shortcuts, such as Ctrl+P sets the page to print.</p> <p><b>Note:</b> The onbeforeprint event is not supported in Safari and Opera.</p> <script> function myFunction() { alert("You are about to print this document!"); } </script> </body> </html>Test it out ‹/›
IEFirefoxOperaChromeSafari
Currently, only Internet Explorer and Firefox browsers support the onbeforeprint event attribute.
The onbeforeprint attribute is triggered when the page is about to be printed (before the print dialog appears).
Tip: The onbeforeprint attribute is usually used together with the onafterprint attribute
The onbeforeprint attribute is a part of HTML5New Attributes.
<element onbeforeprint="script">
Value | Description |
---|---|
script | Script executed in onafterprint. |