简体   繁体   English

Safari:在打印iframe时防止出现两个打印对话框

[英]Safari: Prevent two print dialogs when printing an Iframe

My site has a "print this page" button. 我的网站上有一个“打印此页”按钮。

I load a static print template HTML file into a hidden iframe, copy the HTML into that page using jQuery, and call window.print() from the Iframe page. 我将静态打印模板HTML文件加载到隐藏的iframe中,使用jQuery将HTML复制到该页面中,然后从iframe页面中调用window.print() All is well, except on Safari, which wants to print the parent frame as well, so I get two print dialogs opening. 一切都很好,但Safari也要打印父框架,所以我打开了两个打印对话框。

I've tried calling window.print from within the iframe, and calling it from the parent, targetting the iframe ( document.printFrame.window.print() ) but I get two dialogs regardless. 我试过从iframe中调用window.print,并从父级中调用window.print,以iframe为目标( document.printFrame.window.print() ),但是无论如何我都会得到两个对话框。

Does anyone know a way around this? 有谁知道解决这个问题的方法吗? I only want to print the Iframe, not the parent. 我只想打印iframe,而不是父级。

I can't reproduce the bug; 我无法重现该错误; it works fine for me (ie: I get only one Print dialog), regardless of whether I call the it from the iframe or the parent frame. 不管我是从iframe还是从父框架调用它,它对我来说都很好(即:我只有一个“打印”对话框)。 Maybe you are calling window.print() twice somewhere? 也许您在某个地方两次调用window.print()?

I am running Safari 4.0.3 on Mac OS X 10.6 我在Mac OS X 10.6上运行Safari 4.0.3

EDIT: Here it is: http://jsfiddle.net/Kq9dc/ 编辑:这是: http : //jsfiddle.net/Kq9dc/

EDIT 2: I just tested this on Safari 5.0/Windows 7 and it is working correctly. 编辑2:我刚刚在Safari 5.0 / Windows 7上对此进行了测试,并且可以正常工作。 Are you sure it isn't something else in your code? 您确定代码中没有其他内容吗?

EDIT 3 : Just tested this on several versions of Safari on WinXP: 编辑3 :刚刚在WinXP的多个版本的Safari上进行了测试:

Safari 3.0 (first beta): Not working (no print dialog)
Safari 3.1 (first non-beta): Works fine
Safari 4.0: Works fine
Safari 5.0: Works fine

Try this. 尝试这个。 Put this in said iframe: 将其放在所说的iframe中:

function printPage() { print(); }

then in the parent: 然后在父级中:

function printIframe(id)
{
    var iframe = document.frames ? document.frames[id] : document.getElementById(id);
    var ifWin = iframe.contentWindow || iframe;
    iframe.focus();
    ifWin.printPage();
    return false;
}

Should this not work either, I'd try opening up a new window/tab, and fill it with the printable HTML. 如果这也不起作用,我将尝试打开一个新的窗口/选项卡,并用可打印的HTML填充它。

var printwin = window.open("about:blank", "_new");
printwin.document.open();
printwin.document.write("HTML goes here..javascript which is going to print is in there too..");
printwin.document.close();

Two minor things to watch out for are 需要注意的两个小事情是

  • split </script> in the new window, so you don't terminate prematurely 在新窗口中分割</script> ,这样就不会过早终止
  • use setTimeout() to execute after onLoad() has finished, otherwise Firefox users may see a blank page below the print dialog 使用setTimeout()在onLoad()完成后执行,否则Firefox用户可能会在打印对话框下方看到空白页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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