Problem when redirecting page, after printing using jQuery

Asked

Viewed 266 times

1

Guys I’m giving a window.print(); to print my page, soon after it I send the window.location to redirect the page, the problem and the page and redirect before the print command. I need to make the redirect work only when the user prints or cancels the print.

Can someone help me? Follows my code:

$(document).ready(function () {

        // Remove o loading
        $(".se-pre-con").hide();

        // Remove Menu topo
        $(".header").hide();
        $('.content').css({
            top: ("0px")
        }).show();

        // Imprime a página
        window.print();

        // Redireciona página
        window.location = 'Logado.php?pagina=<?= $pagina_crypt ?>';
    });

2 answers

1

You can try the function setTimeout to hold:

$(document).ready(function () {

        // Remove o loading
        $(".se-pre-con").hide();

        // Remove Menu topo
        $(".header").hide();
        $('.content').css({
            top: ("0px")
        }).show();

        // Imprime a página
        window.print();

        // Redireciona página após meio segundo
        setTimeout(function(){
             window.location = 'Logado.php?pagina=<?= $pagina_crypt ?>';
        }, 500);

    });
  • increased to }, 1000); and it worked. Thank you

  • However I noticed that it takes 1000ms to redirect, after closing the window. It is possible to redirect as soon as the user closes the window?

  • @Hugoborges, Unfortunately I only achieved this by adjusting the time, a little for more or a little for less until checking the best configuration with this implementation.

0

I got a better solution.

        // Imprime a página
        window.print();
                
        (function () {
            var beforePrint = function () {
                
                // Remove o loading
                $(".se-pre-con").hide();

                // Remove Menu topo
                $(".header").hide();
                $('.content').css({
                    top: ("0px")
                }).show();
            };
            var afterPrint = function () {
                // Redireciona página
                window.location = 'Logado.php?pagina=<?= $pagina_crypt ?>';
            };

            if (window.matchMedia) {
                var mediaQueryList = window.matchMedia('print');
                mediaQueryList.addListener(function (mql) {
                    if (mql.matches) {
                        beforePrint();
                    } else {
                        afterPrint();
                    }
                });
            }

            window.onbeforeprint = beforePrint;
            window.onafterprint = afterPrint;
        }());

Source: https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

  • you tested on Google Chrome?

  • This implementation is not supported by Chrome, Opera or Safari: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onafterprint

  • @Allanandrade I tested here and it worked in every browser

Browser other questions tagged

You are not signed in. Login or sign up in order to post.