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: