admin管理员组文章数量:1355671
I'm trying to implement a basic popup window that asks the user if they really want to leave a page, similar to what would happen on this site if I tried to close the window half way through writing this message.
I realise this is something that is generally frowned upon but I have good reason for wanting to do it.
I have got it working by using the following code:
function confirm_exit(e) {
if(!e) e = window.event;
e.cancelBubble = true;
e.returnValue = 'Are you sure you want to leave?';
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
However, what I would really like to do is display the message whenever they leave the page, UNLESS they leave by clicking one of two links.
(I've just realised that sounds like I might want to force them to click an advert or something!)
The reason for using this is at the end of a booking process, where users can either confirm their booking or add further bookings before making the confirmation. (These would be the two possibilities that I would NOT like the popup message to display, the message in the pop up would just say something like 'Your booking is not yet confirmed, are you sure you wish to leave?).
Is there anyway to achieve this?
Any advice appreciated.
Thanks.
I'm trying to implement a basic popup window that asks the user if they really want to leave a page, similar to what would happen on this site if I tried to close the window half way through writing this message.
I realise this is something that is generally frowned upon but I have good reason for wanting to do it.
I have got it working by using the following code:
function confirm_exit(e) {
if(!e) e = window.event;
e.cancelBubble = true;
e.returnValue = 'Are you sure you want to leave?';
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
However, what I would really like to do is display the message whenever they leave the page, UNLESS they leave by clicking one of two links.
(I've just realised that sounds like I might want to force them to click an advert or something!)
The reason for using this is at the end of a booking process, where users can either confirm their booking or add further bookings before making the confirmation. (These would be the two possibilities that I would NOT like the popup message to display, the message in the pop up would just say something like 'Your booking is not yet confirmed, are you sure you wish to leave?).
Is there anyway to achieve this?
Any advice appreciated.
Thanks.
Share Improve this question asked Jul 1, 2010 at 8:17 DanDan 3872 gold badges5 silver badges8 bronze badges 2- 2 possible duplicate of How to display onbeforeunload dialog when appropriate? – kennytm Commented Jul 1, 2010 at 8:19
- Does this answer your question? How to display onbeforeunload dialog when appropriate? – ggorlen Commented Jan 4, 2020 at 13:50
2 Answers
Reset to default 2At the head:
<head>
...
<script type="text/javascript">
var disabledConfirm_exit=false;
</script>
</head>
In the two links allowed to leave, you can add
onclick="disabledConfirm_exit=true;"
And inside the confirm_exit
function confirm_exit(e) {
if(disabledConfirm_exit) return;
if(!e) e = window.event;
e.cancelBubble = true;
e.returnValue = 'Are you sure you want to leave?';
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window .unload function will help us to execute some javascript while closing the browser or redirecting to any other page. resources: http://api.jquery./unload/
本文标签: popupJavascriptConfirmation when leaving pageStack Overflow
版权声明:本文标题:popup - Javascript - Confirmation when leaving page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744053143a2582783.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论