Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML | DOM onbeforeprint Event

  • Last Updated : 20 Jul, 2021

The onbeforeprint event occurs when a user gives a command to print a page. A dialog box will display just before printing the page. The onbeforeprint event is the opposite of the onafterprint event.
Supported Tags

  • <body>

Syntax: 
 

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

  • In HTML: 
     
<element onbeforeprint="myScript">
  •  
  • In JavaScript: 
     
object.onbeforeprint = function(){myScript};
  •  
  • In JavaScript, using the addEventListener() method: 
     
object.addEventListener("beforeprint", myScript);

Below examples illustrate the onbeforeprint Event in HTML DOM:
 

  • Example: Using HTML 
     

html




<!DOCTYPE html>
<!DOCTYPE html>
<html>
 
<body onbeforeprint="myFunction()">
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
         
<p>Try to print this page you will see a alert</p>
 
        <script>
            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
    </center>
</body>
 
</html>
  • Output: 
    After giving the print command: 
     



  • Example: Using javascript 
     

html




<!DOCTYPE html>
<html>
 
<body>
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
         
<p>Try to print this page you will see a alert</p>
 
        <script>
            document.getElementsByTagName("BODY")[0].onbeforeprint = function() {
                myFunction()
            };
 
            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
  </center>
</body>
 
</html>
  • Output: 
    After giving the print command: 
     

  • Example: In JavaScript, using the addEventListener() method: 
     

html




<!DOCTYPE html>
<html>
 
<body>
    <center>
        <h1 style="color:green">
          GeeksforGeeks
      </h1>
         
<p>Try to print this page you will see a alert</p>
 
 
        <script>
            window.addEventListener("beforeprint", myFunction);
 
            function myFunction() {
                alert("You are going to print this page");
            }
        </script>
    </center>
</body>
 
</html>
  • Output: 
    After giving the print command: 
     

Supported Browsers: The browser supported by HTML DOM onbeforeprint event are listed below: 
 

  • Google Chrome 63.0
  • Internet Explorer
  • Firefox

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :