在不打开新标签的情况下打印外部URL

时间:2018-09-25 15:33:54

标签: javascript jquery html

我正在尝试弹出外部URL的浏览器打印选项,而不在新选项卡中打开它。

 <a href="##" onclick="printBGReport();" align="center">Print Report</a> <br/>
  <script>
    function printBGReport(){
        var W = window.open(document.getElementById('reportBGID').src);
        W.window.print();            
    }
  </script>

此代码工作正常;但是,它将打开一个带有打印选项的新选项卡。另外,如果我使用Chrome,则当前的标签页在打开新标签页后就会消失。

1 个答案:

答案 0 :(得分:0)

您可以使用0x0 iframe来执行此操作。诸如此类(未经测试):

<script>
var iframe = document.getElementById("my-iframe");

iframe.contentWindow.innerHTML = "<strong>hello world</strong>";

iframe.print();

</script>

<style>
#my-iframe {
    width: 0;
    height: 0;
}
</style>
相关问题