0

Hi,

The onUnload event is triggerred both when a user clicks the 'Refresh' button(post back) as well as when the user clicks the 'X' (close browser)icon on the browser.

I have to build an application in which the page may post back while drop-downlist selected index change event occurs.I need to pop up confirm box only when the browser is closed,but now in post back also the event will trigger.

can any one advise a method to trigger unload event only when browser is closed.

Thanks in advance.........

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by jd31068
1

The only thing I can think of is adding a variable that you check to see if you should execute unload event at all.


Below is sudo code for what I'm thinking.

var isPostBack = false;
$(function(){
  //you would copy this for select and any other form elements I forgot about
   $('input').live('click', function(){
      isPostBack = true;
   });
});

function pageUnload(){
   if(!isPostBack){
      //do your unload code
   }
}

...


<body onunload="pageUnload();">
...

Hope it works, I haven't tested, just kinda gone through the logic in my head.

0
<html>
<script language="javascript" type="text/javascript">
 
        window.onbeforeunload = function() 
{
            if ((window.event.clientX < 0) || (window.event.clientY < 0) || (window.event.clientX < -80))
 {
 
               alert("hai");
            }
        };
 
    </script>
<body onbeforeunload = function() >
test
</body>
</html>

This worked out in IE.

0

No, this will not yield the results you want. What happens if the mouse is out of the main window and you refresh? What if the window isn't maximized and the mouse is to the left of the window? What if someone uses a shortcut to close the window?

0

No, this will not yield the results you want. What happens if the mouse is out of the main window and you refresh? What if the window isn't maximized and the mouse is to the left of the window? What if someone uses a shortcut to close the window?

Yup, what you said is correct.

but do we have solution for this problem.I have trouble implementing the code you have provided.with that code the event gets executed while page refresh also.

Please advice.

0

The only thing I can think of is adding a variable that you check to see if you should execute unload event at all.


Below is sudo code for what I'm thinking.

var isPostBack = false;
$(function(){
  //you would copy this for select and any other form elements I forgot about
   $('input').live('click', function(){
      isPostBack = true;
   });
});

function pageUnload(){
   if(!isPostBack){
      //do your unload code
   }
}

...


<body onunload="pageUnload();">
...

Hope it works, I haven't tested, just kinda gone through the logic in my head.

Thanks for posting this; I utilized this today to help me to know when a postback is causing the page to load. Only thing I added was that if the flag was true (that a postback has happened and therefore don't run my unload code) was to reset the flag back to false.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.