I want to ajax the collected access information to the server when the web page is closed. The existing code is roughly as follows, but it is not feasible in actual testing.
window.onbeforeunload=function(e){
visit_end=new Date();
visit_long=((visit_end.getTime()-visit_start.getTime())/1000).toFixed(1);
//此处省略以上采集的访问信息
ajax_visit_info(); //在关闭网页时,调用ajax函数发送到后台。
//return confirm('你真的要关闭吗?'); //不想加上这个,看起来不友好。
}
Actual test, the background did not receive it at all...! Egg! pain!
What is the method? ? Without affecting the friendliness...
onbeforeunload is unreliable, some browsers do not support it, and they do not support asynchronous tasks in callbacks.
Service workers require modern browser support
So let’s trigger it based on other events
Only Service Worker
This should be data reporting. You can take a look at the implementation of Baidu reporting