JavaScript - Page Printing

JavaScript provides the window.print() method to allow the user to print the content of a web page. When this method is called, it triggers the browser's print dialog, allowing the user to select the printer, set the number of copies, choose the page range, and other printing options.

Here is an example of using the window.print() method:

function printPage() {

  window.print();

}

In the example above, the function printPage is defined and when it is called, it triggers the browser's print dialog. You can call this function using an event handler, such as a button click, or any other suitable trigger.

It's also possible to control the layout and appearance of the page when it's printed by using CSS media queries to apply specific styles for print media:

<style>

@media print {

  /* styles for print media go here */

}

</style>

For example, you can hide elements that are not needed on the printed page, set the page margins, adjust the font size, and more. This way, you can ensure that the printed page looks exactly as you want it to.