javascript - How to return the access behavior to the background through ajax before the web page is closed? ?
天蓬老师
天蓬老师 2017-07-05 11:00:43
0
3
920

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...

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
滿天的星座

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template