Unload event in IE10, no form data

喜欢而已 提交于 2019-12-13 06:20:42

问题


I have window.unload hooked to save my form data in emergencies when the user just closes their browser. I send this using ajax via POST. This works in IE9, Chrome etc, but not in IE10 where the form data is empty (using GET is a workaround).

I can't find any references to this behaviour, is it documented somewhere?


回答1:


I assume, you are using code like this:

<html>
   ...
   <body onunload="inOnUnload();">
      ...

with an inOnUnload()-function being defined like the following:

function inOnUnload() {
   xmlhttp.open("POST", "http://some-location", /*async*/ true);
   http.send(request);
}

The problem with this in IE10 is that it seems to cancel the request, after the document has finally been unloaded. This happens before the form data had a chance to leaf the client. To send data in onunload events in IE10, you must use the async = false parameter in XMLHttpRequest.open(...).

The following works fine for me:

function inOnUnload() {
   xmlhttp.open("POST", "http://some-location", /*async*/ /*!!!*/ false);
   http.send(request);
}


来源:https://stackoverflow.com/questions/16383939/unload-event-in-ie10-no-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!