简体   繁体   中英

Javascript print dialog slow to activate on button click

I have 2 pages, that print in slightly different ways. One always works, and one works about 70% of the time, but I don't understand why one works and the other does not.

The one that does not always work has a button on it (Print and next). Which when clicked invokes the following 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. As soon as the page loads, it calls 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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