How do i print a web page using javascript?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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

    <html>

    <head>

        <title>Javascript print() Methodtitle>

    head>

    <body>

        <h2>GeeksforGeeksh2>

        <h4>Javascript print() Methodh4>

        <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:

    How do i print a web page using javascript?

    Javascript print() Method


    How do I print a web page exactly?

    Open the web page. 2. Click on File – Print preview 3. Change the drop-down selection from “As Laid Out on Screen” to Only the Selected Frame” (this will not show the headers and footers upon printing).

    Is there print in JavaScript?

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

    How do I print a web page in HTML?

    You can easily add a print button to your web page by adding the following code to your HTML document where you want the button to appear:.
    onclick="window.print();return false;" />.
    print..
    type="text/css" media="print" />.
    body {visibility:hidden;} .print {visibility:visible;}.