search

Home  >  Q&A  >  body text

javascript - How to return the access behavior to the background through ajax before the web page is closed? ?

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

天蓬老师天蓬老师2708 days ago919

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-07-05 11:02:43

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 11:02:43

    Only Service Worker

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 11:02:43

    This should be data reporting. You can take a look at the implementation of Baidu reporting

    reply
    0
  • Cancelreply