在stackoverflow中,如果您开始进行更改,那么您尝试离开页面,会出现一个javascript确认按钮并询问:"您确定要离开此页面吗?" blee blah bloo ...
有没有人以前实现过这个,我如何跟踪提交的更改?我相信我自己可以做到这一点,我正在努力学习专家们的良好做法.
我尝试了以下但仍然无法正常工作:
<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>
Run Code Online (Sandbox Code Playgroud)
任何人都可以发一个例子吗
显然,window.onbeforeunload已经遇到了Chrome的问题,正如我从遇到的所有问题中看到的那样.最近的工作是什么?
我唯一接近工作的是这个:
window.onbeforeunload = function () { return "alert" };
Run Code Online (Sandbox Code Playgroud)
但是,如果我用"alert"("blah")之类的东西替换返回"alert",我从Chrome中得不到任何结果.
我在这个问题中看到谷歌有意阻止了这一点.对他们有好处......但如果有人关闭窗口我想要进行AJAX调用怎么办?在我的情况下,我想知道有人离开我的网站上的聊天室,由窗口关闭发出信号.
我想知道是否有办法
(a):修复window.onbeforeunload调用以便我可以将AJAX放在那里
或
(b):获取一些其他方法来确定Chrome窗口已关闭