Open In App
Related Articles

JavaScript Window print() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

JavaScript Window print() Method is a simple code in JavaScript used to print the content of web pages. The print() method prints the contents of the current window. It basically opens the Print dialog box which lets you choose between various printing options.

Syntax: 

window.print();

Parameters:

No parameters are required

Returns:

This function does not return anything

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:

But the web pages are not limited to text only. There are other things too in the webpages like images consisting of different colors, etc. Printing such pages can be done in the following ways : 

  • Make a copy of the page and leave out unwanted text and graphics, then link that to the printer-friendly page from the original. This means that the whole page will be printed as you have seen the page it will be printed as it is without any change in it, if you will see an advertisement it will also be printed on it
  • If you do not want to keep an extra copy of a page, then you can mark your printable text using proper comments as PRINT STARTS HERE ….. PRINT ENDS HERE and then you can use PERL or any other script in the background to purge printable text and display it for final printing. This means that the selected portion will be printed

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Internet Explorer 5 and above
  • Firefox 1 and above
  • Opera 6 and above
  • Safari 1.1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Last Updated : 18 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads