如何在Safari / Chrome中从javascript打印IFrame

时间:2022-10-30 23:46:10

Can someone please help me out with printing the contents of an IFrame via a javascript call in Safari/Chrome.

有人可以通过Safari / Chrome中的javascript调用帮助我打印IFrame的内容。

This works in firefox:

这适用于Firefox:

$('#' + id)[0].focus();
$('#' + id)[0].contentWindow.print();

this works in IE:

这适用于IE:

window.frames[id].focus();
window.frames[id].print();

But I can't get anything to work in Safari/Chrome.

但我无法在Safari / Chrome中使用任何功能。

Thanks

谢谢

Andrew

安德鲁

11 个解决方案

#1


42  

Put a print function in the iframe and call it from the parent.

将打印功能放在iframe中并从父级调用它。

iframe:

IFRAME:

function printMe() {
  window.print()
}

parent:

父:

document.frame1.printMe()

#2


48  

here is my complete, cross browser solution:

这是我完整的跨浏览器解决方案:

in the iframe page:

在iframe页面中:

function printPage() { print(); }

in the main page

在主页面中

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

Update: Many people seem to be having problems with this in versions of IE released since I had this problem. I dont have time to-reinvestigate right now, if youre stuck i suggest you read all the comments in this whole thread!

更新:自从我遇到这个问题以来,许多人似乎在发布的IE版本中遇到了问题。我现在没有时间重新调查,如果你卡住我建议你阅读这整个帖子中的所有评论!

#3


32  

I used Andrew's script but added a piece before the printPage() function is called. The iframe needs focus, otherwise it will still print the parent frame in IE.

我使用了Andrew的脚本,但在调用printPage()函数之前添加了一个部分。 iframe需要焦点,否则它仍会在IE中打印父帧。

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

Don't thank me though, it was Andrew who wrote this. I just made a tweak =P

不要感谢我,是安德鲁写的。我刚做了一个tweak = P.

#4


8  

In addition to Andrew's and Max's solutions, using iframe.focus() resulted in printing parent frame instead of printing only child iframe in IE8. Changing that line fixed it:

除了Andrew和Max的解决方案之外,使用iframe.focus()导致打印父框架而不是在IE8中仅打印子iframe。更改该行修复它:

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

#5


4  

Use firefox window.frames but also add the name property because that uses the iframe in firefox

使用firefox window.frames,但也添加name属性,因为它在firefox中使用iframe

IE:

IE:

window.frames[id]

Firefox:

火狐:

window.frames[name]

<img src="print.gif"  onClick="javascript: window.frames['factura'].focus(); parent['factura'].print();">
<iframe src="factura.html" width="100%" height="400" id="factura" name="factura"></iframe>

#6


3  

One thing to note is if you are testing this locally using file:///, it will not work on chrome as the function in the iframe will appear as undefined. However once on a web server it will work.

需要注意的一件事是,如果您使用file:///在本地测试它,它将无法在chrome上工作,因为iframe中的函数将显示为undefined。但是,一旦在Web服务器上它将工作。

#7


3  

I had to make few modifications in order to make it with in IE8 (didn't test with other IE flavours)

我不得不做一些修改,以便在IE8中使用它(没有测试其他IE口味)

1) document.frames[param] seem to accept a number, not ID

1)document.frames [param]似乎接受一个数字,而不是ID

printIframe(0, 'print');

function printIframe(num, id)
{
  var iframe = document.frames ? document.frames[num] : document.getElementById(id);
  var ifWin = iframe.contentWindow || iframe;

  ifWin.focus();
  ifWin.printPage();

  return false;
}

2) I had a print dialog displayed upon page load and also there was a link to "Click here to start printing" (if it didn't start automatically). In order to get it work I had to add focus() call

2)我在页面加载时显示了一个打印对话框,并且还有一个“单击此处开始打印”的链接(如果它没有自动启动)。为了使它工作,我不得不添加focus()调用

<script type="text/javascript">
  $(function(){
    printPage();
  });

  function printPage()
  {
    focus();
    print();
  }
</script>

#8


0  

You can use

您可以使用

parent.frames['id'].print();

Work at Chrome!

在Chrome上工作!

#9


0  

You can also use

你也可以使用

top.iframeName.print();

or

要么

parent.iframeName.print();

#10


0  

The 'framePartsList.contentWindow.print();' was not working in IE 11 ver11.0.43

'framePartsList.contentWindow.print();'在IE 11 ver11.0.43中没有运行

Therefore I have used framePartsList.contentWindow.document.execCommand('print', false, null);

因此我使用了framePartsList.contentWindow.document.execCommand('print',false,null);

#11


-4  

Use this:

用这个:

window.onload = setTimeout("window.print()", 1000);

#1


42  

Put a print function in the iframe and call it from the parent.

将打印功能放在iframe中并从父级调用它。

iframe:

IFRAME:

function printMe() {
  window.print()
}

parent:

父:

document.frame1.printMe()

#2


48  

here is my complete, cross browser solution:

这是我完整的跨浏览器解决方案:

in the iframe page:

在iframe页面中:

function printPage() { print(); }

in the main page

在主页面中

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

Update: Many people seem to be having problems with this in versions of IE released since I had this problem. I dont have time to-reinvestigate right now, if youre stuck i suggest you read all the comments in this whole thread!

更新:自从我遇到这个问题以来,许多人似乎在发布的IE版本中遇到了问题。我现在没有时间重新调查,如果你卡住我建议你阅读这整个帖子中的所有评论!

#3


32  

I used Andrew's script but added a piece before the printPage() function is called. The iframe needs focus, otherwise it will still print the parent frame in IE.

我使用了Andrew的脚本,但在调用printPage()函数之前添加了一个部分。 iframe需要焦点,否则它仍会在IE中打印父帧。

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

Don't thank me though, it was Andrew who wrote this. I just made a tweak =P

不要感谢我,是安德鲁写的。我刚做了一个tweak = P.

#4


8  

In addition to Andrew's and Max's solutions, using iframe.focus() resulted in printing parent frame instead of printing only child iframe in IE8. Changing that line fixed it:

除了Andrew和Max的解决方案之外,使用iframe.focus()导致打印父框架而不是在IE8中仅打印子iframe。更改该行修复它:

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

#5


4  

Use firefox window.frames but also add the name property because that uses the iframe in firefox

使用firefox window.frames,但也添加name属性,因为它在firefox中使用iframe

IE:

IE:

window.frames[id]

Firefox:

火狐:

window.frames[name]

<img src="print.gif"  onClick="javascript: window.frames['factura'].focus(); parent['factura'].print();">
<iframe src="factura.html" width="100%" height="400" id="factura" name="factura"></iframe>

#6


3  

One thing to note is if you are testing this locally using file:///, it will not work on chrome as the function in the iframe will appear as undefined. However once on a web server it will work.

需要注意的一件事是,如果您使用file:///在本地测试它,它将无法在chrome上工作,因为iframe中的函数将显示为undefined。但是,一旦在Web服务器上它将工作。

#7


3  

I had to make few modifications in order to make it with in IE8 (didn't test with other IE flavours)

我不得不做一些修改,以便在IE8中使用它(没有测试其他IE口味)

1) document.frames[param] seem to accept a number, not ID

1)document.frames [param]似乎接受一个数字,而不是ID

printIframe(0, 'print');

function printIframe(num, id)
{
  var iframe = document.frames ? document.frames[num] : document.getElementById(id);
  var ifWin = iframe.contentWindow || iframe;

  ifWin.focus();
  ifWin.printPage();

  return false;
}

2) I had a print dialog displayed upon page load and also there was a link to "Click here to start printing" (if it didn't start automatically). In order to get it work I had to add focus() call

2)我在页面加载时显示了一个打印对话框,并且还有一个“单击此处开始打印”的链接(如果它没有自动启动)。为了使它工作,我不得不添加focus()调用

<script type="text/javascript">
  $(function(){
    printPage();
  });

  function printPage()
  {
    focus();
    print();
  }
</script>

#8


0  

You can use

您可以使用

parent.frames['id'].print();

Work at Chrome!

在Chrome上工作!

#9


0  

You can also use

你也可以使用

top.iframeName.print();

or

要么

parent.iframeName.print();

#10


0  

The 'framePartsList.contentWindow.print();' was not working in IE 11 ver11.0.43

'framePartsList.contentWindow.print();'在IE 11 ver11.0.43中没有运行

Therefore I have used framePartsList.contentWindow.document.execCommand('print', false, null);

因此我使用了framePartsList.contentWindow.document.execCommand('print',false,null);

#11


-4  

Use this:

用这个:

window.onload = setTimeout("window.print()", 1000);