DEV Community

Discussion on: Sure you want to leave?—browser beforeunload event

Collapse
 
amsharma9 profile image
Amal • Edited

I would like to know if I can differentiate between beforeunload happening when closing window and moving away from page etc. I need to logout the user when s/he closes the window but NOT when s/he is moving to another page etc.

I am using sendBeacon right now as ajax didn't work.

Collapse
 
samthor profile image
Sam Thorogood Google Web Dev

Moving to another page can be detected by watching focus or the Page Visibility API. The page is still open in that case, just hidden or not being used by the user.