Chrome:window.print() 打印对话框仅在页面重新加载后打开(javascript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18622626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:37:46  来源:igfitidea点击:

Chrome: window.print() print dialogue opens only after page reload (javascript)

javascriptgoogle-chromeprinting

提问by oskar1983

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)

我正面临一个非常奇怪的问题。我正在从 javascript 文件调用 window.print()。这在 Safari、IE、Firefox 中运行良好……直到两个小时前,它还在 Chrome 中运行。(版本 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.

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

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.

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

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.

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

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

我很抱歉问,但我在研究时没有找到任何东西。任何帮助,将不胜感激!

Thank you!

谢谢!

回答by noypiscripter

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:

Wasiim 是对的,有一个 Chrome 错误,window.print()<video>DOM 中有一个标签时它不起作用。我通过调用这个函数解决了它:

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;
}

回答by Steven Wilber

From my experience this is due to continued background traffic, e.g. ajax calls and the like that prevent Chrome from feeling the that page is loaded completely. The reload breaks all traffic and thus the print dialog pops up. This is a particular gotcha in Visual Studio 2013 where BrowserLinkcontinually ticks away in the background. This can be tested by disabling BrowserLink via the setting below:

根据我的经验,这是由于持续的后台流量(例如 ajax 调用等)导致 Chrome 感觉该页面已完全加载。重新加载会中断所有流量,因此会弹出打印对话框。这是 Visual Studio 2013 中的一个特殊问题,其中BrowserLink在后台不断滴答作响。这可以通过以下设置禁用 BrowserLink 来测试:

<configuration>
    <appSettings>
        <add key="vs:EnableBrowserLink" value="false"/>
    </appSettings>
</configuration>

回答by Tepliuk

I have exactly same problem with Chrome. You need to manually reload page:

我对 Chrome 有完全相同的问题。您需要手动重新加载页面:

    <a href="javascript:window.print();window.location.reload()">Print</a> 

回答by Francisco

If by any chance someone is using VS2013 with chrome, this problem is caused by the BrowserLink funcionality.

如果有人将 VS2013 与 chrome 一起使用,则此问题是由 BrowserLink 功能引起的。

see SO answer here

在这里看到 SO 答案

回答by Martin

Similar behavior in Safari. It is caused by opened HTTP request(s)on background.

Safari 中的类似行为。它是后台打开的 HTTP 请求引起的

When any HTTP request is in progress, window.print()is executed successfully, but no dialog is opened!

当任何 HTTP 请求正在进行时,window.print()成功执行,但没有打开对话框!

You will have this issue, whenyou use a long polling(for server push). Because client will have already opened HTTP connection for a long time, window.print()will never work.

您使用长轮询(用于服务器推送),您将遇到此问题。因为客户端已经打开了很长时间的 HTTP 连接,window.print()永远不会工作。

回答by Wasiim

I am most certain you are experiencing this issue because you have a video element on your page - most probably an MP4.

我很确定您遇到了这个问题,因为您的页面上有一个视频元素 - 很可能是 MP4。

If you disable this video / or have an OGV video instead, the printing should work fine. It is a bug in chrome itself due to limitations of Chrome's video implementation. It is also important to note that if the user prints manually with ctrl-p / cmd-p, print functions correctly

如果您禁用此视频/或改为使用 OGV 视频,则打印应该可以正常工作。由于 Chrome 的视频实现的限制,这是 chrome 本身的一个错误。还需要注意的是,如果用户使用 ctrl-p / cmd-p 手动打印,则打印功能正常

http://code.google.com/p/chromium/issues/detail?id=141633

http://code.google.com/p/chromium/issues/detail?id=141633

Hope this helps :)

希望这可以帮助 :)