The window print() method is used to print the visible contents of the current window, for example, a web page text or image by displaying the Print Dialog Box which allows the user to choose from a variety of printing options, and the Print Dialog Box is only opened when the print() code is executed.
This will open a Print Dialog Box which will print the visible content of the current window as discussed earlier.
It should be noted that the window.print() method does not take or contain any parameters and also doesn’t return anything.
The window.print() method is supported by many browsers and some of them are as follows:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<h2>Window Print Example!</h2>
<button class="btn">Click Me</button>
In the above code, we are referencing the button in the HTML by using the class anime associated with it. After this, we added an event listener of click which will continuously listen for a click event and whenever someone clicks on the Click Me button, the function within the event listener will run.
When we click on the Click Me button, the Print Dialog box will open:
We can see the content of our webpage on the left-hand side of the Print dialog box and when we click on the print button it will print or save the current viewscreen in a file.