问题
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