JavaScirpt Tutorial Index

JavaScript Tutorial Javascript Example Javascript comment Javascript Variable Javascript Data Types Javascript Operators Javascript If Else Javascript Switch Statement Javascript loop JavaScript Function Javascript Object JavaScript Arrays Javascript String Javascript Date Javascript Math Javascript Number Javascript Dialog Box Javascript Window Object Javascript Document Object Javascript Event Javascript Cookies Javascript getElementByID Javascript Forms Validation Javascript Email Validation Javascript Password Validation Javascript Re-Password Validation Javascript Page Redirect Javascript Print How to Print a Page Using JavaScript Textbox events in JavaScript How to find the largest number contained in a JavaScript Array?

Misc

JavaScript P5.js JavaScript Minify JavaScript redirect URL with parameters Javascript Redirect Button JavaScript Ternary Operator JavaScript radio button checked value JavaScript moment date difference Javascript input maxlength JavaScript focus on input Javascript Find Object In Array JavaScript dropdown onchange JavaScript Console log Multiple variables JavaScript Time Picker Demo JavaScript Image resize before upload Functional Programming in JavaScript JavaScript SetTimeout() JavaScript SetInterval() Callback and Callback Hell in JavaScript Array to String in JavaScript Synchronous and Asynchronous in JavaScript Compare two Arrays in JavaScript Encapsulation in JavaScript File Type Validation while Uploading Using JavaScript or jquery Convert ASCII Code to Character in JavaScript Count Character in string in JavaScript Get First Element of Array in JavaScript How to convert array to set in JavaScript How to get current date and time in JavaScript How to Remove Special Characters from a String in JavaScript Number Validation in JavaScript Remove Substring from String in JavaScript

Interview Questions

JavaScript Interview Questions JavaScript Beautifier Practice Javascript Online Object in JavaScript JavaScript Count HTML Interpreter Getters and Setters in JavaScript Throw New Error in JavaScript XOR in JavaScript Callbacks and Promises in JavaScript Atob in JavaScript Binary in JavaScript Palindrome Number in JavaScript How to Get First Character Of A String In JavaScript How to Get Image Data in JavaScript How to get URL in JavaScript JavaScript GroupBy Methods difference-between-var-let-and-const-keyword-in-javascript JavaScript Beautifier Iterating over Objects in Javascript Find the XOR of two numbers without using the XOR operator Method Chaining in JavaScript Role of Browser Object Model (BOM) in JavaScript Advanced JavaScript Interview Questions Filter() in JavaScript For Loop in JavaScript Google Maps JavaScript API Hide and Show Div in JavaScript How to Find Object Length in JavaScript Import vs. Require in JavaScript JavaScript Frontend Frameworks JavaScript Goto JavaScript Image Compression JavaScript Obfuscator JavaScript Pop() Method JavaScript replaceAll() JavaScript URL Encode JavaScript vs ReactJS JQuery Traversing Regular Expression for Name Validation in JavaScript Switch Statement in JavaScript

How to Print a Page Using JavaScript

JavaScript is one of the most widely used programming languages globally, recognized for its portability, universality, and ease of understanding. It serves as a crucial scripting language for the internet, particularly in creating dynamic websites. JavaScript, a client-side scripting language used by all Modern browsers, enables web servers to access source code, send it over the World Wide Web to clients' computers, and then execute code directly in browsers, thereby facilitating the analysis of page behavior. Additionally, it is utilized for user event processing and checks. The present web page can be printed and stored in a format that can be transported thanks to Javascript.

This post will go over how to print a web page in Javascript and explain its implementation using an example.

Using Window print() Method

The "window.print()" method in JavaScript allows users to print the content of the current webpage. Here's an explanation:

The print() method is used to initiate the printing of the current webpage, including text, images, visuals, and other content. It is typically triggered by a user action, such as clicking a print button.

The printing operation occurs when the browser finishes rendering the page. If the print-related dialog doesn't open during page loading, it may not open afterward. It's crucial to invoke the print method when the page is fully loaded to ensure a smooth printing experience.

The print()method is the part of window object, and it can be used to print content of a window. The information from the active window is printed using the print function. The print window appears, allowing the user to choose the necessary printing options.. This approach becomes blocked if the print method is invoked when the file is still not fully loaded.

Syntax:

window.print(); 

Parameter: There are no parameters required in this method.

The value returned: Nothing is returned.

Implementation Example

Create a button with an onClick event tied to the printPage() function to print a page. This button should activate when the page is printed. The printpage() function (in the script tag) will be invoked whenever the user hits the button. This method may contain some code that aids in printing the page. A dialogue window with the ability to save the file will show up.

Example: This instance shows how to output the present-day webpage in Javascript using the print() method.

Code:

<!DOCTYPE html>

<html>

<head>

    <title>JavaScript print() Method</title>

</head> 

<body>

    <h2>Tutorials and examples</h2>

    <h5>Javascript print() Method</h5>

    <p>

       An instructional portal for technical subjects

        There are effective, thoughtful, and clearly described technological and computer articles.

    </p>

    <button onclick="printPage()" style="color:white;

                   background-color: red;

                   font-weight: bold;">Click

    </button>

    <script>

        function printPage() {

            window.print();

        }

    </script>

</body>

</html>

Output:

How to Print a Page Using JavaScript

Making use of a print library

Here, we add the print JavaScript library file to our HTML. For printing the file, this file's custom method is called. Users can access to the language listed below to utilise this library's features.

Syntax:

<script rel = "stylesheet"  href="CSS source url"/>

<script src = "javascript source url"> </script>

printJS('html','id');

Here, the individual print method is invoked in the subsequent syntax, and the necessary CSS and JavaScript files are supplied using the initial two structures.

Parameters:

  • id: The attribute id is used to describe value of the HTML material's property to print.
  • HTML - The kind of print material, Files or photos may also be included.

Example

We have loaded the necessary modules here. The print-only content ID is the first argument passed to the customized function in a library when the user hits the button. The user is presented with the printing box.

Code:

<html>

<head>

   <script src="https://printjs-4de6.kxcdn.com/print.min.js"></script>

   <link rel="stylesheet" href="https://printjs4de6.kxcdn.com/print.min.css" />

</head>

<body>

   <h2>printing a page using JSP</h2>

   <div id="libInp">

      <p>Tutorials</p>

      <p>and</p>

      <p>examples</p>

   </div>

   <div id="libBtnWrap">

      <h4>Click on the botton</h4>

      <button onclick="libPrint()">Print</button>

   </div>

   <p id="libOp"></p>

   <script>

      var libInpEl = document.getElementById("libInp");

      var libOutEl = document.getElementById("libOp");

      var libBtnWrapEl = document.getElementById("libBtnWrap");

      function libPrint() {

         libOutEl.innerHTML = "Printing the document...";

         libBtnWrapEl.style.display = "none";

         printJS('libInp', 'html');

      }

   </script>

</body>

</html>

Output:

How to Print a Page Using JavaScript