JavaScript print page Specifies the div area principle: Use window.open () to open a new page (window) in the browser, and use Window.document.write () to write the contents of the specified Div area to the new window document. Document.close () Closes the document and uses Window.print () to call the printer to print the current document.
JavaScript print function Myprint (obj):
Copy Code code as follows:
function Myprint (obj) {
Open a new Window NewWindow
var newwindow=window.open ("Print Window", "_blank");
The contents of the DIV to print
var docstr = obj.innerhtml;
Print content write to NewWindow document
NewWindow.document.write (DOCSTR);
Close Document
NewWindow.document.close ();
Calling a printer
Newwindow.print ();
Close NewWindow Page
Newwindow.close ();
}
Myprint () Call method:
Copy Code code as follows:
Myprint (document.getElementById (' Printdivid '));
Instance code:
Copy Code code as follows:
<script>
function Myprint (obj) {
var newwindow=window.open ("Print Window", "_blank");
var docstr = obj.innerhtml;
NewWindow.document.write (DOCSTR);
NewWindow.document.close ();
Newwindow.print ();
Newwindow.close ();
}
</script>
<div id= "Print" >
Print the demo area, click on the print will load the contents of the new window!
</div>
<button onclick= "Myprint (document.getElementById (' print ')" > Print </button>