简体   繁体   中英

window.print() refreshes page in iframe when I click cancel

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM