Cover Image for JavaScript print() method
103 views

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:

JavaScript
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:

HTML
<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.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS