admin管理员组文章数量:1302970
I have to prompt a user when he/she tries to exit the browser window. For example, when you go to GMail and you start posing a new message, and then try to close it before saving anything, GMail will give you a popup saying something like:
Do you really want to exit this page?
and then you have a choice of either leaving the page or staying on it. So what I am interested in is how they did it and what trick they used (if any) when the user presses Stay on this page
.
I have to prompt a user when he/she tries to exit the browser window. For example, when you go to GMail and you start posing a new message, and then try to close it before saving anything, GMail will give you a popup saying something like:
Do you really want to exit this page?
and then you have a choice of either leaving the page or staying on it. So what I am interested in is how they did it and what trick they used (if any) when the user presses Stay on this page
.
- 4 I just want to say, this can be annoying for some users. Do you really need it? Couldn't you save the info (somewhere), without having to do this? – Mateen Ulhaq Commented Jan 26, 2011 at 19:45
- Please don't do this. Signed, all internet users. – theflowersoftime Commented Mar 20, 2014 at 17:27
2 Answers
Reset to default 7To avoid annoying users, you would probably want to have the state to be changeable, depending on if an operation is running. Something like this:
window.beforeunload = function(){
if(isPerformingOperation) {
return 'Are you sure you want to leave?';
}
}
Set isPerformingOperation
to true when you want to prompt them, and turn it off when you are done.
Bind a function to onbeforeunload
.
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
Should work in all browsers (tested in IE 8, FF 3.6, Chrome 9).
Or with jQuery:
$(window).bind('beforeunload', function(){
return 'Are you sure you want to leave?';
});
本文标签: javascriptShow popup when user tries to exit the browser (GMail style)Stack Overflow
版权声明:本文标题:javascript - Show popup when user tries to exit the browser (GMail style) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741744302a2395444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论