Programming/JavaScript

[jquery]영역프린트하기, window.print();

대나무숲의 개발자 2022. 9. 5. 16:02
반응형

[jquery]영역프린트하기, window.print();

INTRO

 

web image viewer의 세번 째 기능 : 이미지 프린트1

 

 

 

이미지 뷰어의 기능 중 웹에서 프린트하는 기능입니다. 이 기능은 javaScript의 기본 기능으로 아주 간단한 정도의 프린트 제어를 할 수있습니다.

 

장점 : ActiveX 가 필요 없음

단점 : ActiveX 를 사용하지 않으므로 각 개인PC의 설정을 가져와 사용 할수 없음

        -  예를 들어 머리말/꼬리말 등의 제어, 기본프린터로 설정된 프린터로 바로인쇄 등

 

 

 

CONTENTS

 

 

<스크립트 부분>

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<script language="JavaScript"> 
    function print() { 
      if (document.all && window.print) { 
        window.onbeforeprint = bef; 
        window.onafterprint = aft; 
        window.print(); 
      } 
    } 
    
    function bef() { 
      if (document.all) { 
        contents.style.display = 'none'
        printArea.innerHTML = document.all['printArea'].innerHTML;
      } 
    }
 
    function aft() { 
      if (document.all) { 
        contents.style.display = 'block'
        printArea.innerHTML = ""
      } 
    } 
</script>
cs
 
<화면 내용>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<DIV ID="contents"> 
    <input type="button" value="인쇄" onclick="print();">
    <DIV id="printArea" >
      <%--
       프린트할 내용을 코딩합니다.
       --%>
    </DIV>
</DIV>
 
<DIV ID="printArea">
       <%--
       실제 프린트가 되는 영역입니다. 
           contents는 display가 none이 되며 현재영역에 소스코드를 복사하여 인쇄합니다.
       --%> 
</DIV> 
cs

 

 

 

OUTRO

※ ActiveX를 이용한 프린트 기능은 다음 포스팅으로...

 

 

출처 : http://zekill.pe.kr/blog/83

반응형