onbeforeunload : differentiate between different events

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    onbeforeunload : differentiate between different events

    Hi all ,

    I was just going through the onneforeunload property here:

    eSports News, Results, upcoming Matches & live Matches. Learn tricks and guides in the esports space. ✅ We cover CS:GO, Dota 2, LOL, Overwatch & PUBG. 



    Now , consider a scenario such as this :

    my website is located at say www.mywebsite.com which has several web pages for which i want to have the onneforeunload event . Now , assume that all the web pages are including a js file where i can have the code for the event which is like this :

    Code:
    var needToConfirm = true;
    window.onbeforeunload = confirmExit; 
      function confirmExit()
      {
     // Handle what to do
      }
    now , the event can fire in either of the following cases:
    1) Tab is closed
    2) User navigates to another page within the same websote.
    3) window is closed
    4) User navigates to another page outside the domain of www.mywebsite.com


    How to handle all these cases separately? To be more specific , how can i tell whether the user has navigated within or outside the domain of the website?

    Any help appreciated.

    Regards,
    Rhitam
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    This won't be possible using JavaScript cross-browser. You could perhaps use a hack for it to work partially in one browser, e.g. detect the close 'event' in IE, but that's about it.

    Comment

    Working...