how to use callback in window.print()

Asked

Viewed 550 times

0

Well I created a printing function in jQuery, when running it hides from the menu div and prints the page. It works perfectly, but in some browser like IE and iPhone safari it doesn’t work.

I noticed that the error occurs because of the function of displaying the menu again after printing. I think one way to solve this is to have the menu displayed in the callback of the print. Someone knows how to do this?

Follows the code:

        /* Função para imprimir */
        function imprimir() {

            // Remove Menu topo
            $(".header").hide();
            $(".sidenav").hide();
            $(".shadow-full").hide();
            $(".relatorio caption").show();
            $('body').css({"padding-top": "0px"});

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

            // Adiciona Menu topo
            $(".relatorio caption").hide();
            $(".header").show();
            $('body').css({"padding-top": "40px"});
        }
  • You’re printing with the Divs that should be hidden, right? Wouldn’t it be the case to re-display only after printing?

  • Yes she is displaying at print time. Good I have to make her come back after printing.

  • Try using . Promise() - https://api.jquery.com/promise/

  • Can you post an example? if it works already mark your answer.

  • Instead of hiding the elements when printing with js, use only css to show in the print only what you want.

  • how do I do that?

  • See in the answer if it suits you. I think quieter than using js.

Show 2 more comments

1 answer

1


Try just with css

@media print
{    
    .no-print
    {
        display: none;
    }
  
    .print-only {
  
    display: inline;
    
    }
  
}

@media screen {
.print-only {
display: none;}
  }
<div class="no-print">
  Só aparece na tela
  
</div>

<div class="print-only">

  Aparece só na impressão
  
</div>

<div>

  Aparece em ambos os casos
  
</div>

<input type="button" value="imprimir" onclick="window.print();" />

  • 1

    Okay your way makes more sense even hahahah, vlw

  • 1

    I’ve been there! hahaha And I’ve always used js before knowing this possibility in css

Browser other questions tagged

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