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

这在Firefox:

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

此在IE:

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

但我不能得到任何Safari /铬中工作。

由于

安德鲁

有帮助吗?

解决方案

把打印功能在iframe和从父调用它。

IFRAME:

function printMe() {
  window.print()
}

父:

document.frame1.printMe()

其他提示

下面是我的完整,跨浏览器的解决方案:

在的iframe页:

function printPage() { print(); }

在主页

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

    iframe.focus();
    ifWin.printPage();
    return false;
}

更新:许多人似乎是一种在,因为我有这个问题发布了IE的版本与此问题。我没有时间,现在重新调查这一权利,但是,如果你坚持,我建议你在这整个线程读取所有的意见!

我用安德鲁的脚本,但的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;
}

不要谢我,虽然,它是安德鲁是谁写的这一点。我只是做了TWEAK的 = P

在除了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;
}

使用火狐window.frames但因为其使用在Firefox的IFRAME还添加name属性

IE:

window.frames[id]

火狐:

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>

需要注意的一点是,如果你正在测试这个本地使用文件:///,它不会在Chrome作为工作在iframe的功能将显示为未定义。但是在Web服务器上,一旦它会工作。

我不得不做出一些修改,以便与以使其在IE8(不与其他IE口味测试)

1)document.frames [参数]似乎接受一个数,而不是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)我不得不在网页加载时显示的打印对话框,也有一个链接“点击这里开始打印”(如果它没有自动启动)。为了得到它的工作我不得不添加对焦()调用

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

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

您可以使用

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

在镀铬的工作!

您还可以使用

top.iframeName.print();

parent.iframeName.print();

在 'framePartsList.contentWindow.print();'未在IE 11 ver11.0.43工作

因此,我已经使用 framePartsList.contentWindow.document.execCommand( '打印',假,NULL);

使用这样的:

window.onload = setTimeout("window.print()", 1000);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top