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.........

Member Avatar for stbuchok

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.

<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.

Member Avatar for stbuchok

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?

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.