开发者

Using beforeunload to execute task before page exit

开发者 https://www.devze.com 2023-02-09 00:17 出处:网络
I\'m trying to get an action executed before page leave, this requires 开发者_Python百科a GET request being executed.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消