HTML onbeforeprint Event Attribute


The event attribute onbeforeprint is triggered after a user requests a print operation (via a menu or a key combination) immediately before a document is printed, as well as when a user requests a print preview. Use the onbeforeprint event to change the document just before printing or before printing. In most cases, this attribute is used to make all information about the document visible immediately before printing.


<element onbeforeprint="script">


  • scriptThe name of the script to use when the event has been triggered.


<!DOCTYPE html>
<meta charset="utf-8">
<title>onbeforeprint event</title>
<body onbeforeprint="test()">
<h1>Try to print this document.</h1>
<p><b>Tip:</b>The keyboard shortcut Ctrl + P prints the page.</p>
<p><b>Note:</b>The onbeforeprint event is supported only in Internet Explorer and Firefox.</p>
function test() {
    alert("You are about to print this document!");

Supported Tags


Browser Support

Explorer Edge Chrome Firefox Opera Safari
Tablets / Mobile
Android Chrome Firefox Opera Safari Samsung