The window.print()
method in JavaScript is used to open the Print Dialog Box, which allows users to print the contents of the current window or document.
Window print() Method Syntax
window.print();
Window print() Method Parameters
No parameters are required
Window print() Method Returns Value
This function does not return anything
Window print() Method Example
This example shows a print button and then it shows the property of that webpage in a sheet in which you are going to print it.
<!DOCTYPE html> < html lang = "en" >
< head >
< title >
JavaScript Window print() Method
</ title >
</ head >
< body >
< h2 >HI GEEKSFORGEEKS USER'S</ h2 >
< form >
< input type = "button"
value = "Print"
onclick = "window.print()" />
</ form >
</ body >
</ html >
|
Output:
Explanation
- In this example we directly embedded in HTML using the button’s onclick attribute.
- Clicking the button triggers window.print() method, invoking the browser’s print functionality.
- window.print() opens print dialog for users to print current web page content.
- This method facilitates printing without needing a separate print button or functionality.
JavaScript Window print() Method – UseCases:
1. How to print the content of current window using JavaScript ?
window.print() method is used to open the Print Dialog Box to print the current document. It does not have any parameter value.
2. Print the content of a div element using JavaScript
To print a div’s content in JavaScript, store div content in a variable, then on button click, extract and write div content to a popup window, and print using window.print().