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);
//此處省略以上采集的訪問(wèn)信息
ajax_visit_info(); //在關(guān)閉網(wǎng)頁(yè)時(shí),調(diào)用ajax函數(shù)發(fā)送到后臺(tái)。
//return confirm('你真的要關(guān)閉嗎?'); //不想加上這個(gè),看起來(lái)不友好。
}
Actual test, the background did not receive it at all...! Egg! pain!
What is the method? ? Without affecting the friendliness...
歡迎選擇我的課程,讓我們一起見(jiàn)證您的進(jìn)步~~
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
This should be data reporting. You can take a look at the implementation of Baidu reporting