想要打开新窗口,然后在该窗口中显示/的打印对话框

时间:2017-09-13 17:54:12

标签: javascript html dom

我有以下函数被锚点调用onclick。它打开了我想要的新窗口,但问题是打印对话框不会显示,除非open()函数的'URL'字段为空。

我需要能够打开特定网址('https://www.example.com/file.pdf'),因此不能将其留空。

<script>
  var pdfw;
  function printPdf() {
    // pull url
    url = document.getElementById('print').getAttribute('href');
    console.log("pull");

    // open window
    pdfw = window.open('', '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');
    console.log("open");

    // print window
    pdfw.focus().print();
    console.log("print");
  }
</script>

打开新窗口,该窗口为空白,因为当然没有指定URL,并且打印对话框:

pdfw = window.open('', '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');

显示所需文件的新窗口,但不显示打印对话框:

pdfw = window.open(url, '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');

我想说这是一种竞争条件问题,但是没有其他任何东西在脚本运行的任何页面上使用打印功能,所以我很确定不是它。< / p>

非常感谢有关此问题的任何帮助或建议!

2 个答案:

答案 0 :(得分:1)

您可以通过以下方式进行操作。

var win = window.open("", "Print Page", "height=600,width=800");
win.document.write("<h1>Welcome to Print Page</h1>");
win.print();

<强> JsFiddle Here

答案 1 :(得分:0)

打开窗口后,您可以致电

 pdfw = window.open(url, '_blank', 'fullscreen=1,channelmode=1,status=1,resizable=1');
 pdfw.focus();
// you need to call print after window loads like this
 pdfw.onload=function(){
                pdfw.print();
              }

有关详细信息,请参阅this