0

Here is my JS code which will be executed when close the browser or the tab. Which will makes the user status as offline before closing the browser

function checkBrowser(){
   function warning(){
        if($('#loginOrNot').val() == 'loggedIn'){
          setTheExpertStatusToOffline();
          cleanUpChat();
          alert("You are leaving the page");
        }
   }
   window.onbeforeunload=warning;
} 

on documnet.ready() am calling checkBrowser() method, so that it will bind the function for browser close. Now the problem is this is working perfectly fine in all browsers but not in Chrome. Whats wrong in here?

Seeker
  • 2,405
  • 5
  • 46
  • 80
  • Have you tried this: http://stackoverflow.com/questions/1631959/browser-window-close-event ? – Alex W Aug 02 '12 at 13:39

1 Answers1

0

You must return a string at the end of your function. The borwser will then show a confirmation box, including your message.

http://jsfiddle.net/fxtBC/

function warning(){
    if(true){
      console.log('leaving');
      return "You are leaving the page";
    }
}
window.onbeforeunload = warning;​ 
Robin Drexler
  • 4,307
  • 3
  • 25
  • 28
  • Thanks Robin.. that very useful.. how can i execute the setTheExpertStatusToOffline(); cleanUpChat(); statements upon clicking leave page? – Seeker Aug 03 '12 at 04:06
  • Just execute them before returning the string, the console.log in my example is there to demonstrate that. – Robin Drexler Aug 05 '12 at 19:54
  • if i execute them before leaving the page, and if the user clicks on the stay on page button then where can i revert those steps? – Seeker Aug 06 '12 at 12:12
  • Thats a good question and might be worth to be asked on so :) – Robin Drexler Aug 06 '12 at 19:52