Using beforeunload to execute task before page exit - Javascript/jQuery

两盒软妹~` 提交于 2019-12-23 05:24:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!