简体   繁体   English

单击按钮时激活Javascript打印对话框的速度很慢

[英]Javascript print dialog slow to activate on button click

I have 2 pages, that print in slightly different ways. 我有2页,其打印方式略有不同。 One always works, and one works about 70% of the time, but I don't understand why one works and the other does not. 一个始终有效,而一个大约70%的时间都有效,但是我不明白为什么一个有效而另一个无效。

The one that does not always work has a button on it (Print and next). 一个并不总是有效的按钮上面有一个按钮(“打印”和“下一个”)。 Which when clicked invokes the following javascript. 单击时将调用以下javascript。

    <script language="javascript" type="text/javascript">
        function HideNavAndPrint() {
            $('.btn-primary').hide();
            window.print();
        }
    </script>

the button then submits the page so that the user is moved on to the next screen. 然后,该按钮提交页面,以便将用户移至下一个屏幕。

The one that always works, when you click print, it posts back to the server, and generates a new page for printing. 一个始终有效的页面,当您单击打印时,它将回发到服务器,并生成一个新页面进行打印。 this page has a meta tag added at the top, so that after 5 seconds it redirects the user to another page. 该页面的顶部添加了一个meta标签,因此5秒钟后,它将用户重定向到另一个页面。 As soon as the page loads, it calls window.print(); 页面加载后立即调用window.print();。 as a startup script. 作为启动脚本。

I can rebuild my page so that the whole system uses the second mechanism if necessary, I just want to understand why calling window.print() as soon as the document loads always works, but calling it from a button click, which then submits the page is sporadic. 我可以重建页面,以便整个系统在必要时使用第二种机制,我只想了解为什么在文档加载后立即调用window.print()总是可以的,但是通过单击按钮调用它,然后提交页面是零星的。

I can't say why exactly it doesn't work, or why it's intermittent other than to say it's not really surprising. 除了说这并不令人惊讶之外,我不能说为什么它完全不起作用,或者为什么它是间歇性的。 Submitting and printing at the same time just sounds wrong. 同时提交和打印听起来很错误。

As a solution to your problem though have you considered returning false and doing the submit after a timeout? 为了解决您的问题,您是否考虑过返回false并在超时后进行提交?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM