Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to ping a server using JavaScript ?
Next article icon

How to print a web page using JavaScript ?

Last Updated : 31 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Javascript is the world most popular lightweight, cross-platform, interpreted compiled programming language, along with a scripting language for web. It facilitates the dynamic functionality that helps to make the interactive website. As all the Morden browsers use the Javascript & is a client-side scripting language that helps webserver to read the source code & transmit it over the internet to the client’s computer & run directly on browsers, which, in turn, helps to analyze the behavior of the webpage. It is also used for validations and functionality for user events. Javascript facilitates the ability to print the current web page, which can be saved in the portable document format. In this article, we will discuss how to print the web page in Javascript, along with understanding its implementation through the example. 

The print() method prints the content of the current webpage that may includes the text, images, graphics, etc. If the document is still loading while this function is called when the document ends up the loading process before the print dialog is opened. This method gets blocked while opening the print dialog.

Syntax: 

window.print();

Parameter: This method does not take any parameter.

Return value: It does not return any value.

Approach: Create a button with an onClick event that is attached with the printpage() method, & it should be triggered when we want to print the page. When the user clicks the button then printpage() method (in the script tag) will be called, which may contains some code that helps to print the page. After this, a dialog box will appear that will contains the option to save the document.

Example: This example describe the use of the print() method to print the current webpage in Javascript.

HTML
<!DOCTYPE html> <html>  <head>     <title>Javascript print() Method</title> </head>  <body>     <h2>GeeksforGeeks</h2>      <h4>Javascript print() Method</h4>      <p>         A Computer Science portal for geeks.         It contains well written, well thought         and well explained computer science and         programming articles.     </p>      <button onclick="printpage()" style="color:white;                    background-color: red;                     font-weight: bold;">Click to Print     </button>          <script>         function printpage() {             window.print();         }     </script> </body>  </html> 

Output:

You can see the output of the project below:

https://media.geeksforgeeks.org/wp-content/uploads/20240726004300/print.mp4


Next Article
How to ping a server using JavaScript ?
author
singh_teekam
Improve
Article Tags :
  • Geeks Premier League
  • JavaScript
  • Web Technologies
  • Geeks-Premier-League-2022
  • JavaScript-Questions

Similar Reads

  • How to Print a Page using jQuery?
    Printing a webpage directly from the browser can be a useful feature for users who want a physical copy of the content. With jQuery, a popular JavaScript library that simplifies HTML document manipulation and event handling, you can easily implement a print function on your webpage. This guide will
    2 min read
  • How to ping a server using JavaScript ?
    Pinging a server is used to determine whether it is online or not. The idea is to send an echo message to the server (called ping) and the server is expected to reply back with a similar message (called pong). Ping messages are sent and received by using ICMP (Internet Control Messaging Protocol). T
    3 min read
  • How to Print a String in JavaScript ?
    In JavaScript, printing a string means displaying the contents of a string variable or literal to the console, webpage, or any other output medium. Strings are a fundamental data type in JavaScript, representing sequences of characters enclosed within single ('') or double ("") quotes. Table of Cont
    2 min read
  • How to create a coming soon page using JavaScript ?
    What is the Coming Soon Page? First, we need to know what is the meaning of the Coming Soon Page, Coming Soon is a term that means something that is going to take place as scheduled or expected to happen in the near future. Hereby, we get to the conclusion that the Coming Soon Page is a type of webp
    3 min read
  • How to Print a Message to the Error Console Using JavaScript ?
    This article will show you how to print a message using JavaScript on the error console. There are three methods to display the error console, these are: Table of Content Using console.error() MethodUsing console.warn() MethodUsing console.info() MethodApproach 1: Using console.error() MethodThe con
    2 min read
  • How to Save a Web Page with Selenium using Java?
    Selenium is widely known for automating browser interactions, but it can also be used to save web pages directly. This capability is particularly useful when you need to archive web content, save dynamic pages that change frequently, or scrape and store HTML for later analysis. In this tutorial, we’
    2 min read
  • How to Take Screenshot of a Div Using JavaScript?
    A screenshot of any element in JavaScript can be taken using the html2canvas library. This library can be downloaded from its official website. The below steps show the method to take a screenshot of a <div> element using JavaScript. ApproachIn this approach, we will create a blank HTML docume
    2 min read
  • How to Add JavaScript in WordPress Page and Post?
    WordPress doesn’t allow you to directly add JavaScript code to your site, you can achieve this by using plugins or manual methods. JavaScript is a popular programming language that allows you to add interactive features to your website. Learning how to add JavaScript to your WordPress site can help
    3 min read
  • How to print the content of current window using JavaScript ?
    The task is to print the content of the current window by using the window.print() method in the document. It is used to open the Print Dialog Box to print the current document. It does not have any parameter value. Syntax: window.print()Parameters: No parameters are required Returns: This function
    2 min read
  • How to create Pay Role Management Webpage using HTML CSS JavaScript ?
    In this article, we are going to make a Pay Role Management webpage with Javascript. In this project, we are going to learn and clear the concepts of basic javascript. Prerequisites: The pre-requisites for this project are- ES6 JavaScriptQuery SelectorsApproach: To create our Pay Role Management web
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences