JavaScript print() method

In this section, we will discuss the print() method in the JavaScript language. A print() method is used to print the currently visible contents like a web page, text, image, etc., on the computer screen. When we use a print() method in JavaScript and execute the code, it opens a print dialog box that allows the user or programmer to select an appropriate option for printing the current content of the window.

JavaScript print() method

Syntax

The following syntax is used to print the current content of the window:

In the above syntax, we use the window.print() method that prints the currently visible content of the window screen.

Parameters: It does not contain any parameters.

Returns: The window.print() method does not return anything.

Supported Browser of the print() method

Following are the browsers that support the window.print() method.

  1. Google Chrome
  2. Internet Explorer
  3. Firebox
  4. Opera
  5. Safari

Example 1: Program to print a web page using print() method

In this program, we are using a window.print() method that prints the current visible content in the window screen.

print.html

When we execute the above programming code on any browser, it displays the below image:

JavaScript print() method

In the above image, there is a Print button. Now we click on the Print button, it opens a print dialog box with the current visual content of the window screen, as shown below.

JavaScript print() method

Example 2: Program to print the Student Registration Form using the print() method

In this program, we create a student registration form and then print it using window,print() method.

Student.html

When we execute the above programming code on any browser, it displays the below image:

JavaScript print() method

Now we fill in all the details of the student registration form and click on the Print button, which shows the below image.

JavaScript print() method

After click on the Print button, it shows the different operations of the window.print method. Once we are done with all settings in the Print dialog box, click on the Save button to save the file or page in your system.

Explanation of the code: In the above program, we have created a student registration form, and this form has two buttons Reset and Print button. The Reset button is used to reset the content, and the Print button is used to print the displayed content. When we click on the Print button, an onclick function will activate and call the PrintFun() function. Now PrintFun() function is executed that contains window.print() method to print the displayed content on the window screen.

Conclusion:

We have learned about the print() method in JavaScript that prints the window screen's content. Where the contents of the window screen may be text, image, advertisement, etc. Furthermore, when a print() method is called in JavaScript, it opens the print dialog box. So that, user or programmer can select the appropriate option for printing displayed content on the window screen.






Latest Courses