English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

HTML tag大全

HTML5 onbeforeprint event attribute

The beforeprint event is triggered when the associated document is about to be printed or previewed for printing.

HTML Event Attributes

Online Example

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 ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

Currently, only Internet Explorer and Firefox browsers support the onbeforeprint event attribute.

Definition and Usage

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

HTML 4.01 from HTML5difference

The onbeforeprint attribute is an HTML5New Attributes.

Syntax

<element onbeforeprint="script">

Attribute Value

ValueDescription
scriptScript executed in onafterprint.
HTML Event Attributes