0

When I close the browser, I have to perform some server side functions.

If any exception occurs while performing the server side functions, I have to show an alert message "Operation Unsuccessful" and in that case the browser should not close.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
F11
  • 3,703
  • 12
  • 49
  • 83
  • 1
    This site doesn't just provide you with code; you need to have tried something first, so that we can tell you what you did wrong. – Ja͢ck Oct 18 '12 at 04:59
  • I believe you can not prevent a browser window from being closed. – Uooo Oct 18 '12 at 05:02
  • 1
    hey check this stack overflow question http://stackoverflow.com/questions/10860642/how-to-show-alert-to-user-if-the-user-is-closing-the-browser?lq=1 and also try this http://www.dotnetobject.com/Thread-Close-the-browser-tab-after-alert-message-click-using-javascript – riti Oct 18 '12 at 05:06
  • yes you can not handle the close button of browser. – Vikash Sinha Oct 18 '12 at 05:06
  • Are you by any chance trying to make people write code for you?! Break down your problem into sub-problems.......its easy enough to solve them(and please do Google b4 posting a question). – Sayan Oct 18 '12 at 05:23

4 Answers4

1

Actually, you can NOT prevent the user from closing the window. you can however alert him/her with your message, asking the user to stay or leave the page, this done by binding to onbeforeunload event.

var showAlert = false;
window.onbeforeunload = function(){
    $.ajax({
       url: "serversideprcess.aspx",
       async : false
       }).done(function ( data ) {
        if(data !="success") 
           showAlert = true;
      }).fail(function(){
    showAlert = true;
    });

    if(showAlert){
       return "Operation unsuccessful";
    }
}

the ajax request must be synchronous: async : false

Akram Berkawy
  • 4,920
  • 2
  • 19
  • 27
  • my answer assume that `serversideprcess.aspx` will return `success` string when the operation is successful, otherwise the operation is unsuccessful. – Akram Berkawy Oct 18 '12 at 05:45
  • ,i have a method TEST in serversideprcess.aspx,this method will return true or false.Now how to implement it in above and based on that return type i have to decide whether to display alert or not? – F11 Dec 06 '12 at 10:07
1

You can't.

You cannot prevent a browser window from closing at all. Ever. Neither can you perform any work that takes time (like an HTTP request) within an onbeforeunload handler -- all you're really allowed to do is display a confirmation dialog.

0

Perhaps you can use this code

<html>
<head>
<script type="text/javascript">
  var hook = true;
  window.onbeforeunload = function() {
    if (hook) {
      return "Did you save your stuff?"
    }
  }
  function unhook() {
    hook=false;
  }
 </script>
</head>
<body>

<a href="http://google.com">external link</a>


</body>
</html>
J. Steen
  • 15,470
  • 15
  • 56
  • 63
polin
  • 2,745
  • 2
  • 15
  • 20
0
window.onbeforeunload = WindowCloseHanlder;
function WindowCloseHanlder() {
    //Call ajax server code CallIntiateAjaxCalling();
    if (conditionfails) {
        alert("condition fails")
            return false;
    }
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Sreerejith S S
  • 258
  • 5
  • 18