Problem when redirecting page, after printing using jQuery

1

I'm giving window.print(); to print my page, right after it I send window.location to redirect the page, the problem is that the page is redirected to the print command. I need to make the redirect work only when the user prints or cancels printing.

Can anyone help me? Here is 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 ?>';
    });
    
asked by anonymous 26.08.2016 / 14:58

2 answers

1

You can try the setTimeout function to handle:

$(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);

    });
    
26.08.2016 / 15:01
0

I've 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: link

    
26.08.2016 / 15:32