简体   繁体   中英

window.onbeforeunload vs window.print in Internet Explorer

One of my pages shows a discount code, and if you try to navigate away I use the window.onbeforeunload to alert the user to write down the code because otherwise it will be lost forever. That page has also a print button, so the user can print the voucher.

The problem is that, in Internet Explorer when the user click on the link <a href="javascript:window.print();"> , the event window.onbeforeunload is raised. This doesn't happen in Chrome.

Is there a way to solve this?

I've seen that if I try this:

<a href="javascript:alert('herp');window.print();">

The 'herp' message appears after the warning showed by window.onbeforeunload , so I don't know how to fix this.

只需使用<a onclick="window.print();">代替href属性即可。

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