2012-05-10 3 views
0

このコードはjavascriptから印刷することができます。しかし、それは印刷される文書を含むウィンドウを開きます。その文書を隠す方法はありますか?JavaScriptを使用したバックグラウンドでの印刷、つまりドキュメントポップアップなし

var element=document.getElementById(element_id); 
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100'); 

newWin.document.open(); 
/* newWin.document.title = "Readings on PageLinks"; */ 
newWin.document.write('<html><head><title>Readings on PageLinks</title></head><body onload="window.print()">'+element.innerHTML+'</body></html>'); 
newWin.document.close(); 

setTimeout(function(){ newWin.close(); },10); 

印刷はそのドキュメントのonload()が行われているため、印刷せずに印刷することはできませんでした。しかしそれは隠されることができますか?

+0

をあなたにできることを探していますOSの印刷ダイアログを使わずに印刷するのではなく、ドキュメントの一部を印刷するのですか? –

+0

私は上記で作成した 'newWin.document'を隠す方法を探しています。それをする方法はありますか? – user823527

答えて

1

window.open()を使用しないで、How to print only a selected HTML element?に記載されているような印刷固有のスタイルシートを使用してこれを行うことができます。必要な場合は動的に適用されるCSSクラスを使用して、印刷する必要のある/しない要素を指定します。

0

マークアップにこれを追加します。

<iframe id="ifrOutput" style="display:none;"></iframe> 

はjavascript関数以下を追加します。

function printContainer(content, styleSheet) { 
    var output = document.getElementById("ifrOutput").contentWindow; 
    output.document.open(); 
    if (styleSheet !== undefined) { 
     output.document.write('<link href="' + styleSheet + '" rel="stylesheet" type="text/css" />'); 
    } 
    output.document.write(content); 
    output.document.close(); 
    output.focus(); 
    output.print(); 
} 

そしてそうのようにそれを呼び出す:

// with stylesheet 
printHtml('<div>Styled markup</div>', 'printStyles.css'); 

// without stylesheet 
printHtml('<div>Unstyled markup</div>'); 
関連する問題