开发者

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜