简体   繁体   English

如何在javascript中更改beforeunload事件

[英]how to alter beforeunload event in javascript

Is calling an ajax function possible before someone's decide to leave the page. 是否可以在有人决定离开页面之前调用ajax函数。 I mean i can ask the user if user wants to leave or stay. 我的意思是我可以问用户是否要离开或停留。 But i dont know how to alter behaviour when user click "yes leave the page" button. 但是当用户单击“是,离开页面”按钮时,我不知道如何更改行为。

I have this code working.. 我有这个代码工作..

<head>
<script type="text/javascript">
    function catchExit() {
        return "All your data is going to be lost. Are you sure??";
    }
</script>
</head>
<body onbeforeunload="return catchExit()">
<p>
    Dont leave the page.
</p>
</body>

Here's an alternative way using confirm : 这是使用confirm的另一种方法:

function catchExit(){
  var exit = confirm("Are you sure?");
  if (exit){
  }
}

But keep in mind that AJAX can't stop the thread. 但是请记住,AJAX无法停止线程。 That means that there's no guarantee the call: 这意味着无法保证调用:

  1. Makes it 使它
  2. Is successful 成功了
  3. Returned a result 返回结果

The window will have shown the prompt and/or exited before the AJAX may have even completed. 在AJAX可能尚未完成之前,该窗口将显示提示和/或退出。 To illustrate it better: 为了更好地说明它:

function catchExit(){
  $.ajax({
    success: function(){
      // this is an entirely new thread and returning a result
      // has ZERO effect on the original catchExit function
      // (And chances are it's already executed and is long gone)
    }
  });
  // this (99.99% of the time) will be called long before
  // 'success' above is called, therefore making 'success' and its
  // outcome moot.
  return "There are unsaved changes, are you sure you want to exit?";
}

Actually, i found a way to get my $.ajax() call worked. 实际上,我找到了一种使$.ajax()调用起作用的方法。 Setting async parameter to false works in firefox, chrome, iexplorer and safari. async参数设置为false可以在Firefox,Chrome,iexplorer和Safari中使用。 It only doesn't work in opera. 它仅在歌剧中不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问在卸载前触发的 JavaScript href 事件 - How to access JavaScript href event that triggered beforeunload 如何在JavaScript中取消激活beforeunload事件侦听器 - How to deactivate beforeunload event listener in JavaScript JavaScript'beforeunload'事件在ie中不起作用 - JavaScript 'beforeunload' event not working in ie 如何通过 JavaScript 在 android webview 中监听页面 beforeunload 或 unload 事件 - How to listen the page beforeunload or unload event in android webview by JavaScript (Javascript)替代“beforeunload”事件中的“event.returnValue” - (Javascript) Alternative to "event.returnValue" in "beforeunload" event 如何在 React 上执行 beforeUnload 事件? - How to perform a beforeUnload event on React? JAVASCRIPT:是否可以避免窗口在事件被卸载之前关闭? - JAVASCRIPT: Is that possible to avoid window to close with event beforeunload? 记录用户是否在 Javascript 'beforeunload' 事件上单击“取消” - Record if the user clicked 'cancel' on Javascript 'beforeunload' event 卸载前事件 - Event beforeunload 为什么IE10在使用javascript href单击anchor元素时触发beforeunload事件以及如何防止它 - Why IE10 fires beforeunload event when clicking on anchor element with javascript href and how to prevent it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM