Click a link and print the landing page as soon as it opens

5

I am using self.print() to call print and with this opens the option to print the page that I am. However, I'd like you to go to another page via% a% of% before printing to print ... All this when you click on the " Print " button. Is this possible?

My HTML

<tr>
    <td width="60"><a id="meulink" href="index.php?content=fones-uteis&action=update&id=<?=$uteis['id'];?>">Imprimir</a></td>
    <td width="60"><a href="index.php?content=fones-uteis&action=update&id=<?=$uteis['id'];?>">Editar</a></td>
    <td width="60"><a href="index.php?content=fones-uteis&action=delete&id=<?=$uteis['id'];?>" onclick="return confirmDelete();">Excluir</a></td>
</tr>
    
asked by anonymous 15.05.2015 / 20:51

2 answers

2

When you click "Print" call a function that redirects to another page, on the other page in the load, call the print function.

On the page where you have the link with the text "Print" do this:

JavaScript

$('#id_imprimir').click(function(){   
   window.open('url da sua página aqui', '_blank');
});

HTML

<a href="#" id="id_imprimir">Imprimir</a>

On the other page do this:

$(function(){
  window.print();
});

I did not roll the test I made using the location.href, it did not open in new tab, so I changed. I do not know how your JS framework, this example is simple, just for you to understand how it will work. This will automatically open the print window. Ai with css "media print" you configure what is going to be printed and such.

    
15.05.2015 / 21:16
3

You can test like this:

var el = document.querySelector('a#meulink');
el.addEventListener('click', function(e){
    e.preventDefault();      // impedir que o link seja seguido imediatamente
    var href = this.href;    // colocar em cache o url
    self.print();            // imprimir
    window.location.href = href;  // ir para nova página
});

Strangely, I can not simulate this in jsFiddle or jsBin. But I did a page locally and it worked fine.

    
15.05.2015 / 20:59