1

My Actual Task is to confirm the user when he closes the tab or window. For that I wrote the following code,

 var validNavigation = false;

 function wireUpEvents() {

  var dont_confirm_leave = 0; 
   var leave_message = 'You sure you want to leave?'
  function goodbye(e) {
    if (!validNavigation) {
      if (dont_confirm_leave!==1) {
         if(!e) e = window.event;
        //e.cancelBubble is supported by IE - this will kill the bubbling process.
        e.cancelBubble = true;
        e.returnValue = leave_message;
       //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
          e.stopPropagation();
          e.preventDefault();
        }
        //return works for Chrome and Safari
        return leave_message;
      }
    }
  }
  window.onbeforeunload=goodbye;

  // Attach the event keypress to exclude the F5 refresh
  $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type='button']").bind("click", function() {
    validNavigation = true;
  });

}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
  wireUpEvents();
});

the problem here is it asks confirmation if the user reloads the web page. So i need to bind the reload event.

Please help me... Thanks in advance

3
  • 1
    If the page reload is done following user interaction inside the document, you can use any kind of flag/logic. If you want to handle all case as user pressing F5, there is no way as far as i know Commented Sep 11, 2013 at 11:07
  • There's no way for JavaScript to know where the user navigates to, i.e. whether it is "merely" a reload. BTW: There are other keys that do a reload/navigate than F5, Safari on Mac uses ⌘+R and of course you can do it through the context menu or the application menu, through scripts, refresh headers, browser add-ons, and so on. Catching F5 will lead you nowhere. To JavaScript a page re-load looks just like any other navigation - the dialog will have to pop up. On the other hand bugging the user if they navigate away is rude. Simple solution: Don't do it unless there are unsaved changes. Commented Sep 11, 2013 at 11:20
  • Tomalak : My task is to update the user's login status to false, when he close the web application. So by using my code when user reloads the page then status updated to false. I need to set false only when user close the window. Commented Sep 11, 2013 at 11:59

1 Answer 1

1

Use .unload or .on('unload', handler):

$(window).unload(function() {
  // your code here
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.