
JavaScript print() method
The print()
method in JavaScript is a built-in function that allows you to print the contents of the current web page. When called, it opens the print dialog box of the user’s browser, providing them with options to select the printer and adjust printing settings.
Here’s an example of how to use the print()
method:
function printPage() {
window.print();
}
In this example, the printPage
function is defined, and when called, it invokes the print()
method of the window
object. This triggers the print dialog box, allowing the user to print the current web page.
You can attach the printPage
function to a button or any other element’s event listener to trigger the printing functionality. For example:
<button onclick="printPage()">Print</button>
When the user clicks the “Print” button, the printPage
function will be called, and the print dialog box will be displayed.
It’s important to note that the print()
method is a browser function and relies on the browser’s print capabilities. The appearance and behavior of the print dialog box may vary across different browsers and operating systems.