正如 LapinLove404 在 window.onbeforeunload not working in chrome 中提到的那样,似乎在 Chrome 中的 beforeunload
函数中,除了更改确认消息外,别无他法。
以下代码在 FF 和 IE 中有效,但在 Chrome 中无效:
<html>
<head>
<script type="text/javascript">
window.onbeforeunload = function sair() {
alert("This alert doesn't show up in Chrome");
return "If you leave this page your data will be lost!";
}
</script>
</head>
<body>
<a href="http://www.example.com"> Leave the page </a>
</body>
</html>
我怎样才能在用户离开页面之前做些什么?例如,如果我需要通过异步调用保存页面状态或取消设置先前设置的锁。我认为它有很多用例,但不幸的是 Chrome 并没有让它成为一项微不足道的任务。任何解决方法的想法?
最佳答案
onbeforeunload
的功能非常有限。我认为您最多只能回复一条消息。您无法阻止某人离开您的页面。您不能在 onbeforeunload
中执行任何代码。您可以为此尝试 onunload
。
关于javascript - 如何在 Chrome 中执行 window.onbeforeunload = function(e) {..} 中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934759/