Unload event in IE10, no form data

2019-09-16 04:28发布

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条回答
我只想做你的唯一
2楼-- · 2019-09-16 05:14

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);
}
查看更多
登录 后发表回答