Home >Web Front-end >JS Tutorial >Example of JavaScript printing a specified area of ​​a web page_javascript skills

Example of JavaScript printing a specified area of ​​a web page_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:50:041289browse

Principle of specifying div area for JavaScript printing page: Use window.open() to open a new page (window) in the browser, use window.document.write() to write the content of the specified div area into the new window document, document .close() closes the document, and uses window.print() to call the printer to print the current document.

JavaScript printing function myPrint(obj):

Copy code The code is as follows:

function myPrint(obj){
//Open a new window newWindow
var newWindow=window.open("Print Window","_blank");
//The div to be printed Content
var docStr = obj.innerHTML;
//Print content is written into newWindow document
newWindow.document.write(docStr);
//Close document
newWindow.document.close ();
//Call the printer
newWindow.print();
//Close the newWindow page
newWindow.close();
}

myprint() calling method:

Copy code The code is as follows:
myPrint(document.getElementById('printDivID'));

Example code:

Copy code The code is as follows:
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();
}




Print the demonstration area. After clicking print, the content here will be loaded in a new window!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn