Last Updated : 11 Jul, 2025
The onbeforeprint event fires when a page is about to be printed before the print dialog box appears. This event can be used to perform actions before printing, such as hiding unnecessary elements or preparing the page for printing.
Syntax:<element onbeforeprint = "script">Supported Tags: Attribute Value:
This attribute contains the script, this script is to be run on onbeforeprint
Example 1: In this example, we will see the implementation of the onbeforeprint Event attribute.
html
<!DOCTYPE html>
<html>
<head>
<title>onbeforeprint attribute</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body onbeforeprint="myFunction()">
<h1>
GeeksforGeeks
</h1>
<h2>
onbeforeprint attribute
</h2>
<!-- The script run when page will ready to print -->
<script>
function myFunction() {
alert("This document is ready to be printed");
}
</script>
</body>
</html>
Output:
Example 2: In this example, we will see the implementation of the onbeforeprint Event attribute.
HTML
<!DOCTYPE html>
<html>
<head>
<title>onbeforeprint attribute</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body onbeforeprint="myFunction()">
<h1>
GeeksforGeeks
</h1>
<h2>
onbeforeprint attribute
</h2>
<!-- The script run when page will ready to print -->
<script>
function myFunction() {
console.log("This document is ready to be printed")
}
</script>
</body>
</html>
Output:
Supported Browsers:RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4