JavaScript

JavaScript Window Print Page Button

The “Window Print page Button” is used to print the page’s content. In JavaScript, the predefined method “window.print()” is used for this purpose. By clicking on the window print page button, a dialogue box will appear with the print option. It will print the content of the current window except for unwanted elements such as horizontal menus, navigation bars, ads, and other such elements.

This post will describe the JavaScript window print page button and its functionality.

JavaScript Window Print Page Button

Window Print Page Button” displays a print dialog box to print the current page. This will print all the contents of the page.

Syntax
The syntax of the “Window Print Page Button” is as follows:

window.print()

Example
Create some content with a button in the HTML file. First, create heading using <h3> HTML tag:

<h3>Keep User on the Same Page</h3>

Add a label using <label> tag:

<label>Click the cancel button and stay on the same page</label> <br><br>

After that create a “Print” button by attaching an “onClick()” event that will call the JavaScript “window.print()” method:

<button onclick="window.print()">Print</button>

The output after executing the HTML file looks like this:

Output

While clicking on the “Print” button, the output shows that the print dialog box appears to be printing this page.

After printing, the document looks like this:

The above output shows the content of the printed page, but the “window.print()” method didn’t print the navigation bar.

Conclusion

The “Window Print page Button” can be implemented in JavaScript with the help of the print() method of the window object. This window.print() method only serves one purpose, and that is to print out the contents of the current webpage on the browser. Whenever this method is invoked, it opens the print dialogue box and allows the user to choose the desired print options. However, this window.print() method doesn’t print any navigation bars, ads, and other elements of the same type. This post describes the JavaScript window print page button and its functionality.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.