在stackoverflow中,如果你开始进行更改并尝试离开页面,会出现一个JavaScript确认按钮询问:“确定要离开此页面吗?”仿佛如闻其声...
有人实现过这个功能吗?我该怎么追踪提交的更改?我相信我可以自己完成,我正在尝试从专家那里学习好的做法。
我尝试了以下方法,但仍然无效:
<html>
<body>
<p>Close the page to trigger the onunload event.</p>
<script type="text/javascript">
var changes = false;
window.onbeforeunload = function() {
if (changes)
{
var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
if (confirm(message)) return true;
else return false;
}
}
</script>
<input type='text' onchange='changes=true;'> </input>
</body>
</html>
有人可以提供一个例子吗?