Reputation: 17062
I'm having a rather strange problem when using window.print()
.
I have a page which needs to have a different print layout than what's normally shown to the user. Thus I've created an overlay which is shown when the user clicks the print
button and then it triggers window.print()
. Everything works just fine, except for some unknown reason the print window shows more than once. If I click the print button for the first time I get the print windows 2 times, the second time I get it 4 times.
HTML:
<div id="printDiv" style="position: fixed; overflow: auto; top: 0px; width: 100%; height: 100%; background: white; z-index: 9999;display: none;">
The print page content
</div>
JavaScript:
function show_print(){
$('body').css('overflow', 'hidden');
$('html,body').animate({ scrollTop: 0 }, 'fast', function () {
window.print();
});
}
Any ideas what's causing this?
Upvotes: 3
Views: 2924
Reputation: 11872
I believe it may be because you're selecting two elements thus firing twice as it iterates on each element found within the jQuery selector.
Thus, by adjusting it to $('html')
tag only:
$('html').animate({ scrollTop: 0 }, 'fast', 'swing', function () {
window.print(); //Prints once
});
Fiddle: http://jsfiddle.net/ThFvD/
Upvotes: 6