Best way to detect when a user leaves a web page?

前端 未结 8 2144
太阳男子
太阳男子 2020-11-22 01:57

What is the best way to detect if a user leaves a web page?

The onunload JavaScript event doesn\'t work every time (the HTTP request takes longer than t

8条回答
  •  盖世英雄少女心
    2020-11-22 02:08

    Try the onbeforeunload event: It is fired just before the page is unloaded. It also allows you to ask back if the user really wants to leave. See the demo onbeforeunload Demo.

    Alternatively, you can send out an Ajax request when he leaves.

提交回复
热议问题