2015-07-03 106 views
1

我試圖打開一個new窗口和print的內容。window.print()掛在Internet Explorer中

var parentHeader= $("head").html(); 
var parentDivContent = $("#printer_data_1").html(); 
var newW= window.open("", "", "scrollbars=1,width=200,height=200"); 
    newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>'); 
    newW.document.write('<body><div>'); 
    newW.document.write(parentDivContent); 
    newW.document.write('</div></body></html>'); 
    newW.document.close(); 
    newW.print(); 
    newW.close(); 

當我嘗試運行上述code,它掛在IE8,當我發表意見如下行......它的工作原理。

newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>'); 

我想將整個內容樣式加載到新窗口的頭部分。

+0

我建議你看看這個網址的第三個答案:http://stackoverflow.com/questions/2555697/window-print-not-working-in-ie –

回答

2

想要的全部內容,在 新窗口的頭部被加載風格

嘗試編譯htmlnewW成單一字符串,包括headhtmlparentHeader;在window.open()的第二個參數調用newW的參考設置name;調用document.write單單一html字符串作爲參數

var parentHeader= $("head").html(); 
var parentDivContent = $("#printer_data_1").html(); 
var newW= window.open("", "newW", "scrollbars=1,width=200,height=200"); 
var html = "<html>" 
      + parentHeader // `parentHeader` `head` `html` 
      + "<body><div>" 
      + parentDivContent 
      + "</div></body></html>" 
newW.document.write(html); 
newW.document.close(); 
newW.print(); 
newW.close(); 
1

您可能需要使用innerHTML而不是document.write。這可能是你的問題。

var someVar = 50, 
 
    el = document.getElementById('btn'); 
 

 
function onOpen() { 
 
    var prnt = open(); 
 
    prnt.document.title = 'Page Title'; 
 
    prnt.document.body.innerHTML = [ 
 
    '<h1>', 
 
    'heading of the page', 
 
    someVar, 
 
    '</h1>', 
 
    '<p>A paragraph with text.</p>' 
 
    ].join(''); 
 
    prnt.print(); 
 
    prnt.close(); 
 
} 
 

 
el.attachEvent('onclick', onOpen);
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>Print for Legacy IE</title> 
 
</head> 
 

 
<body> 
 

 
    <a id="btn" href="#">Link</a> 
 

 
</body> 
 

 
</html>