提示并防止用户导航/关闭页面 [英] Prompting and preventing user from navigating away/closing a page
本文介绍了提示并防止用户导航/关闭页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
根据添加或回答问题时StackOverflow的操作方式;
As per how StackOverflow does it when you are adding a question or replying to a question;
我想用一个提示用户
"Are you sure you want to navigate away from this page"
"Press OK to continue or Cancel to stay on this page"
我已经尝试了在ze互联网上找到的几个js代码段,但是没有什么能减少芥末味的.
I've tried a couple of js snippets i've found on ze internet, but nothing quite cuts the mustard.
推荐答案
You need to implement window.onbeforeunload and return your message from it as a string:
window.onbeforeunload = function() {
if (theUserHasStartedEditing) {
return "You have started writing or editing a post."
}
}
这里有一个在线演示: http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm
There's an online demo here: http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm
这篇关于提示并防止用户导航/关闭页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文