HTML onbeforeprint attribute

HTML onbeforeprint attribute: This attribute can be used when a page is about to be printed. And the alert message display before the print dialogue box appears. The onbeforeprint attribute is used with the onafterprint attribute.

HTML onbeforeprint attribute

This attribute is a part of an event attribute. And in HTML 4.01 the attribute has not been defined. It was added newly to HTML5.

Syntax: <body onbeforeprint=”script”>



Browser Support

This attribute supported by different browsers.

  • Chrome-63
  • Internet Explorer
  • Firefox
  • Safari-Not Supported
  • Opera-Not Supported

Example:

<!DOCTYPE html>
<html>
<body onbeforeprint="myFunction()">

<h3>Try to print this document</h3>
<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>

Output:

Try to print this document

Tip: Keyboard shortcuts, such as Ctrl+P sets the page to print.
Note: The onbeforeprint event is not supported in Safari and Opera.