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

Asked

Viewed 5,698 times

5

I’m using self.print() to call print and this opens the option to print the page I am on. However, I would like that before calling the impression could go to another page through the href... All this by clicking the word "Print out".
That’s 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>
  • 1

    If you change page the script stops running and the self.print() will not be run.

  • 1

    And which of the two pages you want to print?

  • The page that will "appear" after clicking on "Print".

  • This printing word is in what type of element? <button>, <a> or another?

  • Is in an <a element>.

2 answers

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
});

Interestingly I can’t simulate this in jsFiddle or jsBin. But I made a page locally and it worked well.

  • I tried this function and checked myself at an infinite loop. .

  • @Fernandaferreira in that browser?

  • I am using Chrome.

  • I also, v42, worked for me. I will investigate further...

  • The code I used is correct?

  • @Fernandaferreira has an error that I corrected in my answer. A comma here this,href; which should be a point. By the way don’t change the question, otherwise the answers will get wrong...

  • @Fernandaferreira tested again with the version without error that I have in the answer?

  • I tested it, looped it again.

Show 3 more comments

3


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 it:

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

The test I did using Location.href didn’t happen here, it didn’t open in a new tab, so I changed it. I don’t know how this JS structure of yours, this example is simple, just so you understand how it will work. That way when falling on the other page will automatically open the print window. Ai with css "media print" you configure what will be printed and such.

  • 1

    The way it was asked seems just that the doubt happens because it could not move (add code) on the destination page that would be printed. But since it was marked as the correct answer... just remember that with this window.print the page will be printed whenever it is accessed, regardless of whether it came from the print button or not.

Browser other questions tagged

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