Using beforeunload to execute task before page exit
I'm trying to get an action executed before page leave, this requires 开发者_Python百科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?
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..
You should prevent default action with event.preventDefault().
Note that not every browser support beforeunload
精彩评论