I have a an iframe inside my website with the following jquery code in it:
$(document).ready(function() {
$('#print_button').click(function() {
开发者_如何学运维 window.print();
});
});
The problem: When I click the print button, the print dialog opens correctly. Printing works correctly as well. But when I click the cancel button in the print dialog it closes but it also refreshes content in the iframe.
How can I avoid this behaviour?
As stated in the above comments, a form is being submitted when the button #print_button
is clicked. This would force the page to refresh.
To prevent this, add a return value of false
to the onclick
event. This would cancel the form submission.
window.print();
return false;
精彩评论