Chrome:window.print()打印对话框仅在页面重新加载后打开(javascript) [英] Chrome: window.print() print dialogue opens only after page reload (javascript)

查看:1104
本文介绍了Chrome:window.print()打印对话框仅在页面重新加载后打开(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个非常奇怪的问题。我从JavaScript文件调用window.print()。这在Safari,IE,Firefox ......中运行良好,直到两小时前它也在Chrome中运行。 (版本29.0.1547.57)

我没有更改JavaScript文件中的任何必需内容(真的 - 我只是删除了一些评论...),但现在发生的事情是真的奇怪:在Chrome中,当调用window.print()时,打印对话不会打开。什么都没发生。但是,当我按下重新加载时,打印对话立即打开。



其他浏览器的行为没有改变。在Chrome中调试时,我可以看到window.print()按预期方式调用,然后脚本继续执行。只有打印对话框才会显示,直到按下重新加载。



有没有人经历过类似的事情?我也尝试在setTimeout()中调用window.print(),但是这并没有改变任何东西。当我调试应该打印的页面的内容似乎完美加载。

我很抱歉问,但是在研究时我没有找到任何东西。

谢谢!

解决方案

Wasiim是对的,有一个Chrome浏览器的bug,当< video> window.print()不起作用>标记在DOM中。我通过调用这个函数来解决它:

  function printPage(){
window.print();

// Chrome bug的解决方法 - https://code.google.com/p/chromium/issues/detail?id=141633
if(window.stop){
location.reload(); //触发卸载(例如重新加载页面)使得打印对话框出现
window.stop(); //立即停止重新加载$ ​​b $ b}
return false;
}


I am facing a really weird problem. I am calling window.print() from a javascript file. This is working fine in Safari, IE, Firefox... and until two hours ago it worked in Chrome, too. (Version 29.0.1547.57)

I did not change anything essential in my javascript file (really - I just removed some comments...), but what now happens is really weird: In Chrome, the print dialogue does not open when window.print() is called. Nothing happens. But then, when I press reload, the print dialogue opens immediately.

The behaviour in the other browser did not change. And while debugging in Chrome I can see that window.print() is called as expected and the script goes on after that. Only the print dialogue is not shown until pressing reload.

Has anybody ever experienced something like that? I also tried to call window.print() in setTimeout(), but this did not change anything. When I debug the content of the page which shall be printed appears to be perfectly loaded.

I am sorry to ask, but I did not find anything while researching. Any help would be appreciated!

Thank you!

解决方案

Wasiim is right, there is a Chrome bug where window.print() does not work when there is a <video> tag in the DOM. I solved it by calling this function:

function printPage() {
    window.print();

    //workaround for Chrome bug - https://code.google.com/p/chromium/issues/detail?id=141633
    if (window.stop) {
        location.reload(); //triggering unload (e.g. reloading the page) makes the print dialog appear
        window.stop(); //immediately stop reloading
    }
    return false;
}

这篇关于Chrome:window.print()打印对话框仅在页面重新加载后打开(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆