简体   繁体   English

确认浏览器退出/开启浏览器退出技术

[英]Confirm Browser Exit/ On Browser Exit Techniques

I've been looking around for a while and haven't quite found what I've been looking for. 我一直在寻找一段时间,并没有找到我一直在寻找的东西。 My web page stores reports, and the user has the ability to save these reports. 我的网页存储报告,用户可以保存这些报告。

I'm looking for a method, using jquery or the javascript onunload/onbeforeunload to ask the user if they wish to save the report or continue without saving when the browser is closed/navigated from. 我正在寻找一种方法,使用jquery或javascript onunload / onbeforeunload来询问用户是否希望保存报告,或者在浏览器关闭/导航时继续保存而不保存。

Using onbeforeunload I can give the user the ability to stop navigation and manually save, although I would rather the save be done through the dialog box. 使用onbeforeunload我可以让用户能够停止导航并手动保存,但我宁愿通过对话框完成保存。 Another problem with onbeforeunload is that clicking tags calls the onbeforeunload method too. onbeforeunload的另一个问题是点击标签也会调用onbeforeunload方法。

The save is asynchronous and takes a few milliseconds. 保存是异步的,需要几毫秒。

Any suggestions are welcome. 欢迎任何建议。

Regards, Byron Cobb. 此致,Byron Cobb。

That is probably the only time a syncronized Ajax call is reasonable. 这可能是syncronized Ajax调用合理的唯一时间。

window.onbeforeunload = function(){
   $.ajax({
      url:      'somewhere',
      type:     'POST',
      dataType: 'text',
      async:    false,
      data:     'you_data_here',
      success:  function(data){
      }
   });
};

That way you should be able to transfer some ammout of data to your server, while the browser blocks the unload. 这样,您应该能够将一些数据传输到您的服务器,而浏览器会阻止卸载。 That has some downsides. 这有一些缺点。 The most obvious, it's probably not that great user experience depending on how much data to transfer and how long it takes. 最明显的是,它可能不是那么棒的用户体验,这取决于要传输多少数据以及需要多长时间。
Another is that this method is not available in some (older) browsers. 另一个是这种方法在某些(较旧的)浏览器中不可用。

A good alternative is maybe to autosave your data from time to time or after each action. 一个很好的选择可能是不时或在每次操作后自动保存您的数据。

      $(document).ready(function() {
             //below turns off popup on a tags, buttons, :input is for a submit in ie and img tags
             $("a").click(RemoveUnload);
             $("button").click(RemoveUnload);
             $(":input").click(RemoveUnload);
             $("img").click(RemoveUnload);
         });

       RemoveUnload(){window.onbeforeunload = null;}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM