How to print the content of current window using JavaScript ? Last Updated : 26 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print()Parameters: No parameters are requiredReturns: This function does not return anythingExample: This example representing how to print the content of current window using JavaScript. html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>GeeksForGeeks - Print Page</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; background-color: #f0f0f0; } .container { max-width: 800px; margin: 0 auto; background-color: white; padding: 20px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } h1 { color: #4CAF50; } h3 { color: #2196F3; } .btn { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; transition: background-color 0.3s; } .btn:hover { background-color: #45a049; } @media print { .no-print { display: none; } } </style> </head> <body> <div class="container"> <h1>Hello GeeksForGeeks Users</h1> <h3>Print the content of the current window using HTML and JavaScript </h3> <p>This is a sample paragraph that will be included in the printed page. You can add more content here as needed. </p> <form class="no-print"> <input type="button" class="btn" value="Print Page" onclick="printCurrentPage()" /> </form> </div> <script> function printCurrentPage() { window.print(); } // Add a listener for the beforeprint event window.addEventListener('beforeprint', function() { console.log('Printing started'); }); // Add a listener for the afterprint event window.addEventListener('afterprint', function() { console.log('Printing finished'); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to print the content of current window using JavaScript ? M manaschhabra2 Follow Improve Article Tags : JavaScript Web Technologies HTML JavaScript-Questions Similar Reads Print the content of a div element using JavaScript If you want to print the content of a <div> element using JavaScript, you can achieve this by extracting the content and then opening a new window or popup to display and print it. This method involves capturing the content of the <div>, creating a new window, and using the print command 2 min read How to close current tab in a browser window using JavaScript? In this article, we will see how to close the current tab in a browser window using JavaScript. window.close() methodTo make this, we will use window.close() method.  This method is used to close the window which is opened by the window.open() method. Syntax:window.close();But accounting for a secur 1 min read How to get the Width of Scroll bar using JavaScript? Given an HTML document, the task is to get the width of the scrollbar using JavaScript. Approach:Create an element (div) containing a scrollbar.OffsetWidth defines the width of an element + scrollbar width.ClientWidth defines the width of an element.So scrollbar can be defined as width = offsetWidth 3 min read How to calculate Width and Height of the Window using JavaScript ? In this article, we will know to calculate the width & the height of the window using Javascript. Given an HTML document that is running on a Window & we need to find the height and width of the window. The Window innerWidth property is used for returning the width of a windowâs content area 2 min read How to Print a Message to the Error Console Using JavaScript ? This article will show you how to print a message using JavaScript on the error console. There are three methods to display the error console, these are: Table of Content Using console.error() MethodUsing console.warn() MethodUsing console.info() MethodApproach 1: Using console.error() MethodThe con 2 min read Like