2013-02-11 139 views
0

我有一个功能,我找不到为什么它打印空白页。也只有Opera打印不是空白页面,它打印所有页面。不只是iframe。在Chrome中,如果我使用srcdoc,则不存在问题。 任何帮助,请打印创建的iframe内容。空页

function printpage(src){ 
//create new image tag 
var newImg = new Image(); 
newImg.onload = function(){ loading(); }; 
newImg.src = src; 
newImg.style.height = "100%"; 
newImg.style.width = "100%"; 

var ifrm = null; 
ifrm = document.getElementById('iframe'); 
if(ifrm==null){ 
    ifrm = document.createElement("IFRAME"); 
    ifrm.style.width=853+"px"; 
    ifrm.style.height=1024+"px"; 
    ifrm.style.display="none"; 
    ifrm.id = "iframe"; 
    ifrm.name = "iframe"; 
    ifrm.width = ifrm.height = 0; 
    document.body.appendChild(ifrm); 
}else{ 
    //ifrm.contentWindow.Reset(); 
    document.body.appendChild(ifrm); 
} 
var s = '<img src ="'+src+'" width="100%" height="100%" ></img>'+ 
      '<script>'+ 
       'function printMe(){ '+ 
        'window.print();'+ 
        '}'+ 
      '</script>'; 
ifrm.src = "data:text/html;charset=utf-8," + escape(s); 

console.log("new ifrm is ",ifrm); 

var frameContent = (ifrm.contentWindow || ifrm.contentDocument || ifrm.window); 

console.log("frameContent",frameContent); 
frameContent.focus(); 
frameContent.print(); 

}

回答

0

我发现这个问题:

ifrm.style.display="none"; // The browser can't print content which is not displayed. 

我用ifrm.style.visibility="hidden";

隐藏元素