简体   繁体   English

用户关闭窗口或标签时如何重定向

[英]how to redirect when user closes window or tab

Is there a way to redirect the window or tab when a user closes it? 用户关闭时,是否可以重定向窗口或标签? This does not work (in jQuery): 这不起作用(在jQuery中):

$(window).bind('beforeunload', function() {
   window.location = 'anotherpage.html'
}

Is it even possible? 可能吗? I just want to redirect a user to another page when they close it. 我只想在用户关闭页面时将其重定向到另一个页面。

No, the only thing you can do is alert the user they are about to close your page and ask them if they would like to remain or confirm to leave. 不,您唯一可以做的就是提醒用户他们即将关闭您的页面,并询问他们是否愿意留下或确认离开。

This is very common on pages which require you to save before exiting, like an online exam or survey for example. 这在要求您退出之前先保存的页面上非常普遍,例如在线考试或调查。

  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
      return "You have attempted to leave this page.  If you have made any changes to the fields without clicking the Save button, your changes will be lost.  Are you sure you want to exit this page?";
  }

您无法转到新的网页,但是如果需要做的是打扫房间(例如向服务器发出ajax调用,清除一些cookie等),则可以直接在onbeforeunload处理程序上执行此操作,而无需提示用户,无需重定向。

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

相关问题 当用户关闭标签或窗口时销毁会话 - Destroying a session when user closes tab or window 关于用户何时关闭浏览器选项卡或窗口的问题 - Question about when user closes browser tab or window Javascript 用户关闭 window 的选项卡时发出警报 - Javascript alert when user closes the tab ot the window 如果用户关闭“ window.open()”-对话框,如何在HTML,JS中重定向? - How to redirect in HTML, JS, if user closes “window.open()” - Dialog? 当用户关闭浏览器或选项卡时如何注销用户 - How to logout the user when user closes the browser or tab 在用户关闭选项卡/窗口之前做一些事情 - Do something before the user closes the tab/window 如何在用户关闭浏览器选项卡时显示弹出窗口? - How to show popup when user closes the browser tab? 当用户在 Angular 9 中关闭浏览器/选项卡时如何执行功能? - How to execute functions when user closes browser/tab in Angular 9? Beforeunload无法将用户重定向到另一个页面,如何在尝试关闭窗口/选项卡时将用户重定向到另一个页面/ URL? - Beforeunload is not working to redirect user to another page, how can I redirect user to another page/url when he attempts to close a window/tab? 当用户关闭移动浏览器窗口时如何采取行动? - How to take action when user closes mobile browser window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM