根据StackOverflow在添加问题或回复问题时如何执行此操作;
我想用一个提示用户;
"Are you sure you want to navigate away from this page"
"Press OK to continue or Cancel to stay on this page"
Run Code Online (Sandbox Code Playgroud)
我已经尝试过在互联网上找到的几个js片段,但没有什么可以减少芥末.
您需要实现window.onbeforeunload并将其作为字符串从中返回:
window.onbeforeunload = function() {
if (theUserHasStartedEditing) {
return "You have started writing or editing a post."
}
}
Run Code Online (Sandbox Code Playgroud)
这里有一个在线演示:http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm