JavaScript +卸载前

发布于 2024-08-02 04:32:36 字数 106 浏览 7 评论 0原文

我对我的申请有疑问。每当用户意外关闭浏览器窗口时,我想在此之前执行一些清理操作。我已经使用了 onunload 事件,但问题是该事件有时会触发,有时不会。我该怎么办,有没有更好的方法来处理此类问题。

I have a query regarding my application. Whenever user closes browser window accidentally I'd want to do some clean up operations before that. I have used onunload event but problem is this event is sometimes is firing and sometimes doesn't. What should I do, is there better way to handle such type of problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅语花开 2024-08-09 04:32:36
window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

请参阅有关 onbeforeunload 的 MSDN 文章

另外还有类似问题 在SO中

window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

See the MSDN article on onbeforeunload

Also there is a similar question in SO

骑趴 2024-08-09 04:32:36

尝试:

window.onbeforeunload = function() { ...your code... }

Try:

window.onbeforeunload = function() { ...your code... }
反话 2024-08-09 04:32:36

根据我的经验,onunload 在不同浏览器中的工作方式有所不同。为什么不使用另一个名为 onbeforeunload 的事件处理程序呢?它应该有效。 Onbeforeunload 将在窗口关闭之前首先执行,因此应该可以解决您的问题。

From my experience onunload works differently in different browsers. Why dont you use another event handler called onbeforeunload instead. It should work. Onbeforeunload will execute first before the window closes, so that should solve your problem.

情释 2024-08-09 04:32:36

这是有效的:

window.onbeforeunload = function(){
    console.log('closing shared worker port...');
    return 'Take care now, bye-bye then.';
  };

例如,如果您正在使用 SharedWorkers 并且您需要告诉工作人员需要管理的端口少了,这会很方便 - 否则,您最终可能会刷新一个浏览器选项卡并得到类似“成功”的信息连接到:10 个其他选项卡”,因为一个选项卡使工作线程保持活动状态,而另一个选项卡的刷新则不断将新的端口连接推送到您的阵列。

希望这可以帮助。

This works:

window.onbeforeunload = function(){
    console.log('closing shared worker port...');
    return 'Take care now, bye-bye then.';
  };

This is handy if, for instance you're working with SharedWorkers and you need to tell the worker that there is one less port to manage -- otherwise, you could end up refreshing one browser tab and get something like a "Successfully connected with: 10 other tabs" because the one tab is keeping the worker alive while the other tab's refreshes keep pushing new port connections to you array.

Hope this helps.

浅唱々樱花落 2024-08-09 04:32:36

出于安全原因,这是不允许的。 onbeforeunload 是一个浏览器 API,在不同的浏览器上有不同的行为。

This is not allowed because of security reasons. onbeforeunload is an browser API that have different behaviors on different Browsers.

淡淡離愁欲言轉身 2024-08-09 04:32:36

这是我的答案之一,仅当用户想要关闭窗口并在某种程度上劫持浏览器消息时才给你 onbeforeunload.....

<html>
<head>
<script src='http://code.jquery.com/jquery-1.6.2.min.js'></script> <!--this is get jquery header file its not required but i have used jquery for minor correction-->
<script type="text/javascript" src="http://fbug.googlecode.com/svn/lite/branches/firebug1.4/content/firebug-lite-dev.js"></script><!--this will give a firebug to check the console in IE-->
<script language='javascript'>
window.onbeforeunload = function(evt){
  var message = "";
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;  //validating the browser where its chrome or not
  if(is_chrome){
    message = "Do you want to close the browser or not?";
  }
  if(this.flag == 1 || $('#help').css('background-color') == 'red'){
    console.log("red");
    return message;
  } else { console.log("blue"); return NULL;}
}
function change(){
  $('#help').css('background-color','red');
  this.flag = 1;
}
</script>

</head>
<body> 
  <a id='help' onclick=change() title="please click on Links">Links</a> 
</body>
</html>

我认为它可能对任何人都有用,谢谢......

this is one of my answer that gives u the onbeforeunload only if the user want to close the window and also to hijack the browser message for some extent.....

<html>
<head>
<script src='http://code.jquery.com/jquery-1.6.2.min.js'></script> <!--this is get jquery header file its not required but i have used jquery for minor correction-->
<script type="text/javascript" src="http://fbug.googlecode.com/svn/lite/branches/firebug1.4/content/firebug-lite-dev.js"></script><!--this will give a firebug to check the console in IE-->
<script language='javascript'>
window.onbeforeunload = function(evt){
  var message = "";
  var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;  //validating the browser where its chrome or not
  if(is_chrome){
    message = "Do you want to close the browser or not?";
  }
  if(this.flag == 1 || $('#help').css('background-color') == 'red'){
    console.log("red");
    return message;
  } else { console.log("blue"); return NULL;}
}
function change(){
  $('#help').css('background-color','red');
  this.flag = 1;
}
</script>

</head>
<body> 
  <a id='help' onclick=change() title="please click on Links">Links</a> 
</body>
</html>

i think it might be useful for any body thanks.....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文