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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 16:50:04
Original
1333 people have browsed it

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!



Related labels:
Statement of this Website
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template