Thursday, September 4, 2025
HomeLanguagesJavascriptHow to create a pop-up to print dialog box using JavaScript?

How to create a pop-up to print dialog box using JavaScript?

Given an HTML document and the task is to design a button that would pop-up a print dialog box. We are going to use JavaScript to do the assigned task:

  • Approach::
    • Add a button which links to a JavaScript Function.
    • Inside the JavaScript Function, use the JavaScript default function to call the print dialog box

    Syntax:

    window.print

    Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>create a pop-up to print 
          dialog box using JavaScript</title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">neveropen</h1>
            <script>
                function printPopUp() {
                    alert("Pop-up dialog-box")
                    window.print();
                }
            </script>
      
            <button onclick="printPopUp()">Print</button>
        </center>
    </body>
      
    </html>

    
    

    Output:
    Before:

    After:

  • Approach::
    • Use DOM onload Event in body tag.
    • Use window alert method for pop-up dialog-box and window.print to print the document.

    Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>create a pop-up to print
          dialog box using JavaScript</title>
    </head>
      
    <body onload="alert('Pop-up dialog-box');window.print();">
        <center>
            <h1 style="color:green">
              neveropen
          </h1> 
          create a pop-up to print dialog box using JavaScript
      </center>
    </body>
      
    </html>

    
    

    Output:

  • Approach::
    • Use <a> href attribute to hyperlink text.
    • Use window alert method for pop-up dialog-box and window.print to print the document.

    Example:




    <!DOCTYPE html>
    <html>
      
    <head>
        <title>create a pop-up to print 
          dialog box using JavaScript</title>
    </head>
      
    <body>
        <center>
            <h1 style="color:green">
              neveropen
          </h1>
            <a href="javascript:alert('Pop-up dialog-box');window.print();">
              Click Me
          </a>
      </center>
    </body>
      
    </html>

    
    

    Output:
    Before:

    After:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS