如何使跨浏览器的窗口卸载请求? [英] How to make a cross-browser on-window-unload request?

查看:173
本文介绍了如何使跨浏览器的窗口卸载请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Flash游戏,我试图保存用户关闭浏览器选项卡的状态。它使用以下jquery代码:

  //当窗口关闭时从Flash调用
函数sendRequest(url,params ){
$ .ajax({
type:POST,
async:false,
url:url,
data:params
})

$ b $(window).unload(function(){
//当Flash关闭时,让Flash试图保存游戏
// Flash获取必要的数据并调用sendRequest()
document [flashGame]。saveBeforeUnload();
});




  • Firefox:正常工作

  • Chrome浏览器:在重新加载时正常工作,而关闭标签页关闭浏览器
  • 时无法正常工作
  • IE所有版本):根本不起作用



我希望它可以在所有浏览器中正常工作,但最重要的是Chrome我们的用户有IE)。

Flash正确地调用了sendRequest(在所有的浏览器中都用一个alert来测试),所以我不认为问题来自Flash,但它可能。

解决方案

简短的回答是,你不能。

onbeforeunload 事件最初是由Microsoft引入的允许标准的确认对话框。它现在被大多数浏览器以原始形式支持,并且在大多数情况下允许执行一个简短的非交互式功能(例如,您可以在localStorage中记录您的状态)。但是,如跨浏览器的jQuery API 中所述,这是不可靠的:


unload事件的确切处理从版本到
版本的浏览器有所不同。例如,某些版本的Firefox在跟随链接时会触发
事件,但当窗口关闭时不会触发
事件。在
的实际用法中,行为应在所有支持的浏览器
上进行测试,并与专有的beforeunload事件进行对比。



<由于在用户要求结束页面(以及脚本)时执行任务有很多潜在的安全问题,这可能是不可靠的,直到(以及如果)严格的标准化工作完成以精确完全可以在 onbeforeunload 回调中完成。



对于可预见的未来,建议不要依赖 onbeforeunload ,但要使用其他方案,例如不断的后台保存或一个大的保存按钮。


I have a Flash game that I'm trying to save the state of when the user closes the browser tab. It is using the following jquery code:

//Called from Flash when window closes
function sendRequest(url, params) {
    $.ajax({
      type: "POST",
      async: false,
      url: url,
      data: params
    })
}

$(window).unload(function() {
  //Make Flash attempt to save the game when the window closes.
  //Flash gets the necessary data and calls sendRequest()
  document["flashGame"].saveBeforeUnload();
});

  • Firefox: Works correctly
  • Chrome: Works correctly when reloading but not when closing tabs or closing the browser
  • IE (all versions): Does not work at all

I want it to work in all browsers correctly, but most important is Chrome (not many of our users have IE).

Flash is correctly calling sendRequest (in all browsers, tested with an alert), so I don't believe the problems come from Flash, but it might.

解决方案

The short answer is that you can't.

The onbeforeunload event was initially introduced by Microsoft to allow a standard confirmation dialog. It is now supported in the original form by most browsers and in most case a short, non interactive, function is allowed to execute (for example you may log your state in the localStorage). But, as described in the cross-browser jQuery API, this is unreliable :

The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietary beforeunload event.

As there are many potential security problems related to the execution of a task when the user asked to end the page (and thus the script), this will probably be unreliable until (and if) a serious normalization effort is done to precise what exactly can be done in a onbeforeunload callback.

For the predictable future, it's recommended to not depend on onbeforeunload but to use other schemes, for example constant background saving or a big "save" button.

这篇关于如何使跨浏览器的窗口卸载请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆