Open In App

JavaScript Window print() Method

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

HTML




<!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().

Supported Browsers:


Similar Reads

Difference between window.location.href, window.location.replace and window.location.assign in JavaScript
Window.location is a property that returns a Location object with information about the document's current location. This Location object represents the location (URL) of the object it is linked to i.e. holds all the information about the current document location (host, href, port, protocol, etc.) All three commands are used to redirect the page t
4 min read
Javascript Window Open() &amp; Window Close() Method
The Javascript Window.Open() method is used to open the web pages into a new window or a new tab. It depends on the browser settings and the values that are assigned to the parameter. Syntax: window.open(URL, name, specs, replace) Parameters: This method accepts four parameters as mentioned above and described below: URL: It is an optional paramete
3 min read
Javascript Window Blur() and Window Focus() Method
Javascript Window Blur() method is used to remove focus from the current window. i.e, It sends the new open Window to the background. Syntax: Window.blur() Parameter: It does not require any parameters. Return Value: It does not return any value. Below examples illustrate the window.blur() method of JavaScript: Example: The below example illustrate
2 min read
Window | Window.requestAnimationFrame() Method
Nowadays, various web development technologies are introduced to create elegant web pages. While talking about elegant web pages, one of the most important things that come to mind is animation. There are many ways to generate them, modern CSS is one of them but JavaScript has always been a power-packed option to do so. The requestAnimationFrame()
3 min read
Web Window API | Window locationbar property
In Web API Window.locationbar property returns the object of locationbar, for which we can check the visibility. Syntax: objectReference = window.locationbar Example: Check the visibility. &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window locationbar property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; }
1 min read
Web Window API | Window scrollbars property
In Web APIWindow.scrollbars property returns the object of scrollbars, for which we can check the visibility. Syntax : objectReference = window.scrollbars Example : Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window scrollbars property &lt;/title&gt; &lt;style&gt; a:focus { background-color: magenta; } &lt;/st
1 min read
Web Window API | DOM Window fullscreen property
The Window interface has a fullscreen property which tells whether the window is displayed in fullscreen mode or not. Syntax: BoolVal = windowReference.fullScreen; Return value: Boolean True: If windows is in fullscreen mode. False: If windows are not in fullscreen mode. Example : &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window
1 min read
Web Window API | Window personalbar property
In Web API Window.personalbar property returns the object of personalbar, for which we can check the visibility. Syntax: objectReference = window.personalbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window personalbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function
1 min read
Web Window API | Window statusbar property
In Web API Window.statusbar property returns the object of statusbar, for which we can check the visibility. Syntax: objectReference = window.statusbar Example: Check the visibility &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; Window statusbar property &lt;/title&gt; &lt;script type=&quot;text/javascript&quot;&gt; function getvisib
1 min read
Web Window API | Window toolbar property
In HTML Window.toolbar property returns the object of toolbar, for which we can check the visibility. Syntax: objectReference = window.toolbar Example: Check the visibility C/C++ Code &amp;lt;!DOCTYPE html&amp;gt; &amp;lt;html&amp;gt; &amp;lt;head&amp;gt; &amp;lt;title&amp;gt; Window toolbar property &amp;lt;/title&amp;gt; &amp;lt;script type=&amp;
1 min read