简体   繁体   中英

print a created iframe content. Empty page

I have the a function and i couldn't find why does it print empty pages. Also only Opera print not empty page, It prints all page. Not iframe only. In Chrome i don't have problem if I use srcdoc. Any help please

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();

}

I found the problem:

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

I hide the element by using ifrm.style.visibility="hidden";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM