admin管理员组文章数量:1307852
What is the JavaScript code/event(s) that is used by sites like stackoverflow and Gmail to test for the user exiting the page once they have begun editing and try to navigate away?
"Are you sure you want to navigate away from this page?"
What is the JavaScript code/event(s) that is used by sites like stackoverflow and Gmail to test for the user exiting the page once they have begun editing and try to navigate away?
"Are you sure you want to navigate away from this page?"
Share
Improve this question
asked Feb 25, 2010 at 2:24
Phillip SennPhillip Senn
47.7k91 gold badges261 silver badges378 bronze badges
2 Answers
Reset to default 8The event used is called onbeforeunload
.
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<input id="foo"></input>
<script type="text/javascript">
function unloadMessage() {
return "Are you sure you want to leave?";
}
function setConfirmUnload(enabled) {
window.onbeforeunload = enabled ? unloadMessage : null;
}
$(document).ready(function() {
$("#foo").keypress(function() {
setConfirmUnload(true);
});
});
</script>
</body>
</html>
onbeforeunload event. Mozilla provides useful example code. you just want to have a function that:
- Returns a string
- Sets e.returnValue to that string, where e is the argument or window.event.
The string will be used as your custom message.
本文标签: javascriptAre you sure you want to navigateStack Overflow
版权声明:本文标题:javascript - Are you sure you want to navigate? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741850427a2401034.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论