chevron_left
459 points
12 7 5

Javascript print

In this article let us understand the Javascript print method().

print()

Print() is a window object method. It opens a print dialog box to print the contents of the current document and allows the user to select the printing options.

Syntax :

window.print()

Let us consider a simple example

<html>
    <head></head>
    <title>Examples</title>
        <body>
            <p>Print() is a window object method.<br></br>
            It opens a print dialog box to print the contents of the current document and <br></br>
            allows the user to select the printing options.</p>
        <button onclick="printdoc()">Print</button>
    </body>
    <script>
        function printdoc()
        {
            window.print();
        }
    </script>
</html>

Here,

<p> tag to define a paragraph element.
<button> to declare a button element.
  • Defined a function printdoc() on button click(). This method will in turn invokes the window.print() method.
  • When we execute the above code and click on button print it opens a print dialog box which will allow you to set certain properties to print the document.

This is pretty simple and hope this helps!!

More Posts

Javascript parsedouble DivyaN - 1 day
Math.sum javascript DivyaN - 1 day
Javascript Hide button DivyaN - 1 day
Javascript for loop array DivyaN - 3 days
How to check if a button is clicked in javascript DivyaN - Oct 18
Button that redirects to another page html - Javascript DivyaN - Oct 18
Javascript insert character into string at position DivyaN - Oct 18
Remove duplicate characters in a string javascript DivyaN - Oct 13
String filter javascript DivyaN - Oct 12
Javascript count occurrences in an array DivyaN - Oct 11