Print pages in JavaScript

Many times you will love to put a button on your website to print the content of that page through a printer. JavaScript helps you perform this function by using the print function of the window object.

Many times you will love to put a button on your website to print the content of that page through a printer. JavaScript helps you perform this function by using the print function of the window object.

The window.print () function in JavaScript prints the current page when executed. You can call this function directly using the onclick event as in the following example:

For example

Try the following example:

  type = "text/javascript" >   type = "button" value = "Print" onclick = " window . print () " /> 

Result

Follow the above method to see the results

Although it serves the purpose of printing, it is not the recommended way to use. A friendly printer page is actually a page with text, no images, graphics or advertising.

You can create a friendly page printer in the following ways:

Create a copy of the page and remove the excess text, graphics, and then connect to that friendly page printer. You check the following example: Example.

If you don't want to keep an extra copy of the page, you can mark printable text using the same comments as: . and then you can use PERL or any other script in the background to filter printable text and display it for the final print job. You use this method to provide print media to visitors. You check for example: For example.

How to print a page

If you don't see the media on a web page, you can use the browser's standard toolbar to print the site. Follow the following link:

 File → Print → Click OK button . 

According to Tutorialspoint

Previous article: Void keyword in JavaScript

Next article: Objects in JavaScript

4 ★ | 1 Vote