JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

How To Print Page using JavaScript

April 9, 2014 by Krishna Srinivasan Leave a Comment

Some times we would like to print some important content on the web page via printer. The JavaScript provide print () method to print contents of the current window. It implements print() function of the window object. The print() method opens print dialog box which allows to select printing options.

Syntax for Window.print

[code lang=”html”]
window.print ()
[/code]

The print() method don’t have any parameters and doesn’t contain any return values.

JavaScript Print Example

[code lang=”html”]
<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">
function printpage() {
window.print();
}
</script>
</head>

<body>
<form>
<input type="button" value="Print" onclick="printpage()" />

</form>
</body>
</html>

[/code]

  • <script type=”text/javascript”> tag is used to define client side scripting which specifies MIME type of the script.
  • window.print(); line specifies that it is used to print contents of the current window.
  • <input type=”button” value=”Print” onclick=”printpage()” /> tag specifies where user can enter data and onclick attribute triggers when user clicks the mouse button once.

JavaScript Print Demo

  • Save the file as print_page.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML specification.

When the execution process is completed successfully we will get the following output :

JavaScript Print

JavaScript Print 1

Filed Under: JavaScript Tagged With: JavaScript Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved