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

HTML Reference Manual

Complete List of HTML Tags

HTML5 onbeforeprint event property

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

HTML Event Attributes

Online Example

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

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 appears).
Tip: The onbeforeprint attribute is usually used together with the onafterprint attribute

HTML 4.01 compared to HTML5differences

The onbeforeprint attribute is a part of HTML5New Attributes.

Syntax

<element onbeforeprint="script">

Attribute Value

ValueDescription
scriptScript executed in onafterprint.
HTML Event Attributes