내일을 위한 오늘

JS#1. Print a specific part of a webpage with javascript 본문

IT·컴퓨터/javascript

JS#1. Print a specific part of a webpage with javascript

안경쓴루피 2022. 5. 31. 15:14
반응형

 

 

 

 

1. Printing using window.onbeforeprint and window.onafterprint.

var prtContent; // The content you want to print
var initBody;  // Original web page body content

function startPrint (div_id) {
	prtContent = document.getElementById(div_id);
	window.onbeforeprint = beforePrint;
	window.onafterprint = afterPrint;
	window.print();
}

function beforePrint(){
	initBody = document.body.innerHTML;
	document.body.innerHTML = prtContent.innerHTML;
}

function afterPrint(){
	document.body.innerHTML = initBody;
}
<button onclick="startPrint('your div id');">print</button>

 

source : http://erunjoy.com/entry/JavaScript-%EC%9B%90%ED%95%98%EB%8A%94-%EB%B6%80%EB%B6%84-%ED%94%84%EB%A6%B0%ED%84%B0-%ED%95%98%EA%B8%B0-print

 

 

 

 

 

2. Printing using pop-up.

var prtCtnt = document.getElementById("your div id");
var prtOption = 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0';
var WinPrint = window.open('', '', prtOption);
WinPrint.document.write(prtCtnt.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();

 

source : https://stackoverflow.com/questions/12997123/print-specific-part-of-webpage

 

728x90
반응형
Comments