Can I pop up a confirmation dialog when the user is closing the window in Safari/Chrome?

谁说我不能喝 提交于 2019-11-27 02:15:50

This works perfectly for me in both Chrome and Safari:

<html><body><p>Test</p>
<script>window.onbeforeunload = function() { return "Sure?"; }</script>
</body></html>

When I try to close the window, I get the prompt.

StackOverflow itself uses onbeforeunload, and it works fine for me in Safari:

function setConfirmUnload(a){window.onbeforeunload=a?function(){return a}:null}

I checked it in chrome and it seems to be working fine using this:

<body onunload="alert('x');" onbeforeunload="test1();">
AnthonyVO

This question is covered in slightly more detail in another newer StackOverFlow question: Setting onbeforeunload on body element in Chrome and IE using jQuery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!