【问题标题】:MS Edge - window.print() is not frame contextual in MS edgeMS Edge - window.print() 在 MS Edge 中不是框架上下文
【发布时间】:2015-08-08 00:03:45
【问题描述】:

与其他浏览器相比,Microsoft Edge 处理 window.print() 的方式不一致。

在大多数浏览器中,从页面的 iframe 中调用 window.print() 只会打印该 iframe 的内容。但是在边缘它总是会打印整个文档。

这是故意的吗?有解决办法吗?

JSFiddle 上的示例。

iframe.html

...
<body>
    <a href="#" onclick="window.print()">print iframe document</a>
</body>
...

index.html

...
<body>
    <a href="#" onclick="window.print()">print outer document</a>
    <iframe src="iframe.html"></iframe>
</body>
...

【问题讨论】:

标签: javascript html iframe printing microsoft-edge


【解决方案1】:

这是微软的confirmed issue,引用:

Posted by Microsoft on 7/29/2015 at 12:46 AM
We were able to confirm the issue, and will be working to resolve it in a future release

尚无解决方法。

【讨论】:

    【解决方案2】:

    我们找到了一个神奇的解决方案:

    parent.document.getElementsByName("pdfjs-frame")[0].contentWindow.document.execCommand("print", false, null);
    

    ...适用于 IE、EDGE、Chrome。另一个尚未测试。

    【讨论】:

    • 您在哪里运行此代码以及如何运行?如果您打开了多个 PDF iframe,会发生什么? (当我执行这段代码时,我得到Unable to get property 'contentWindow' of undefined or null reference)。
    • @tchelidze 我们有带 src 的 iframe,它可以工作,这行代码就是这样。刚刚在 IE 和 Chrome 中测试过。 @Peter嗨,很抱歉回答迟了。代码的第一部分是parent.document.getElementsByName("pdfjs-frame") 它应该通过名称属性在整个文档中找到 i 框架。所以在这个例子中它会找到
    • @prespic 嗯,有趣,你能看看this
    • @tchelidze 这是关于授权的,这个问题没有什么共同点。抱歉,我无法为您提供帮助。
    • 我收到“权限被拒绝”错误。这适用于 IE,但不适用于 Edge。
    【解决方案3】:

    它也在 IE 中。你可以这样修复它。

    window.top.document.getElementById("iframe-id").contentWindow.focus();
    window.top.document.getElementById("iframe-id").contentWindow.print();
    

    经过测试。 ;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-20
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-18
      • 1970-01-01
      相关资源
      最近更新 更多