Print the page in JavaScript
IT Tricks

Print the page in JavaScript


Many times you will prefer to place a button on your website to print that page content via 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 by 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 result

Although it serves printing purposes, it is not the recommended way to use it. A printer friendly page is really a page with text, no images, graphics or advertisements.

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

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

If you do not want to keep an extra copy of the page, you can bookmark the printable text using appropriate comments like: ….. and then you can use PERL or any other script in the background to filter the printable text and display it for the final print job. You use this method to provide print media to your visitors. You check for example:

How to print a page

If you do not see the above media on a web page, then you can use the standard browser toolbar to print the web page. You follow the following link:

File  Print  Click OK button.

Follow Tutorialspoint

Previous article: Void keyword in JavaScript

Next: Objects in JavaScript

.

Leave a Reply

Your email address will not be published. Required fields are marked *