0

I have a very simple javascript function to print the current page:

<script>
    function printpage() {
        window.print();
        alert("PRINTING...");
    }
</script>

and the call:

<a href="#" onclick="printpage()" data-role="button" data-theme="j">Print</a>

If I remove the alert the print popup box doesn't show until I refresh the page. With the alert there is a short pause and then it shows, I would prefer not to have any alert. I tried doing a console.log instead so an event would happen invisibly to the user and still fire the function but it didn't work.

The solution posted here works great for single divs and fires immediately but I need to print the entire page and would prefer to stick with a simple window.print().

EDIT: More details, when I click refresh, the print dialog pops up and the page does not refresh until you actually close the dialog, similar to the way the dialog doesn't show until I close the alert.

Community
  • 1
  • 1
Jake Sellers
  • 2,350
  • 2
  • 21
  • 40
  • Are you using something like jQuery UI? – Pointy Jul 09 '13 at 19:25
  • 5
    Works [fine to me](http://jsfiddle.net/QxT6m/). – Jeff Noel Jul 09 '13 at 19:26
  • What browser (version) are you using? – Bergi Jul 09 '13 at 19:28
  • @Bergi chrome 27.0.1453.116 m – Jake Sellers Jul 09 '13 at 19:32
  • onclick="printpage();return false;" – Geoff Jul 09 '13 at 19:33
  • just some random thoughts: what happens if you omit the `window` and just write `print()`? Or what happens if you write `this.print()`? – basilikum Jul 09 '13 at 19:39
  • @basilikum `print()` works but with the same behavior and `this.print()` does not work at all. – Jake Sellers Jul 09 '13 at 19:40
  • Ok, I think I remember having a similar problem and it had something to do with that the `document` wasn't closed or was still in use somehow or something like this. That's all I can recall right now but maybe it's something else entirely. Do you use `document.write` to create parts of your page? If so, try to write `document.close()` after everything is written. – basilikum Jul 09 '13 at 19:43
  • @basilikum I'm not using `document.write` but thanks everyone for taking a stab at it. I'll update if I solve this. – Jake Sellers Jul 09 '13 at 19:54
  • Don't you have any errors in your `WebKit Inspector/FireBug` ? I would really like to see the page itself, I'm pretty sure it's a forgotten comma or a similar mistake (that we all do 50 times per day). – Jeff Noel Jul 10 '13 at 12:13

1 Answers1

0

Your source work without the alert. See it at : http://jsfiddle.net/WwXwr/1/. (tested on Chrome 27.0.1453.116 and ie 10 / Windows 8)

It can also be replaced by :

<a href="#" onclick="window.print()" data-role="button" data-theme="j">Print 2</a>
Sirus64
  • 46
  • 4
  • The error must be in a other place in your page. This just confirm that the code works. Try to add details or try to reproduce the problem on a jsfiddle because the code should be correct. – Sirus64 Jul 10 '13 at 03:50