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.
When printing the page, execute JavaScript:
<!DOCTYPE html> <html> <head> <title>HTML onbeforeprint event properties usage (Basic Tutorial Website oldtoolbag.com)</title>/title> <body onbeforeprint="myFunction()"> <h1>Try to print this document/h1> <p><b>Tip:</b>/<b>Keyboard shortcuts, such as Ctrl</b>+P sets the page to print./p> <p><b>Note:</b>/<b>The onbeforeprint event is not supported in Safari and Opera.</b>/p> <script> function myFunction() { alert("You are about to print this document!"); } </script> </body> </html>Test to see ‹/›
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 box appears).
Tip: The onbeforeprint attribute is usually used together with the onafterprint attribute
The onbeforeprint attribute is an HTML5New Attributes.
<element onbeforeprint="script">
Value | Description |
---|---|
script | Script executed in onafterprint. |