There is a very simple problem I haven’t found any solution that solves it. Is there any way to distinguish between:
- leaving the browser window (tab) using link
- typing another URL to the URL bar in the browser
- closing the tab or browser window
I need to bind different Javascript code for those actions. I know I can filter all links by setting them special class, but is there anything working in general?
You can’t detect external events of browser such as user’s clicking on browser’s close button or selecting Exit from menu or typing a new url in address bar.
You can however use
onbeforeunloadevent to your advantage to do something:This will trigger when user navigates to other page by clicking on page’s link, closing browser or leaving current page in any way.