问题
I'm trying to get an action executed before page leave, this requires a GET request being executed.
I would like to give the GET request at least 50-100ms in order to get the request forwarded to the web server.
I'm capturing the beforeunload
action using:
$(window).bind('beforeunload', function() {
send_data();
});
Any tips?
回答1:
You have the highest chance to get your request sent if you make it synchronous (async: false
in the $.ajax()
options if you are using jQuery).
But you cannot be sure it always gets through..
回答2:
You should prevent default action with event.preventDefault().
Note that not every browser support beforeunload
来源:https://stackoverflow.com/questions/4910121/using-beforeunload-to-execute-task-before-page-exit-javascript-jquery