See, a web page being able to even know if they are looking or not is a violation of privacy.
The best you can do is adding a listener to the beforeunload event, which will trigger before the user tries to close the tab, and one to the mouseout event for the document, which will trigger when the mouse leaves the viewport. However, both of those can be disabled/blocked by the user.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
See, a web page being able to even know if they are looking or not is a violation of privacy.
The best you can do is adding a listener to the
beforeunload
event, which will trigger before the user tries to close the tab, and one to themouseout
event for thedocument
, which will trigger when the mouse leaves the viewport. However, both of those can be disabled/blocked by the user.