954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Determine whether 'onUnload' is triggerred by Refresh event or close browser event

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

jijo cleetus
Newbie Poster
16 posts since Jan 2011
Reputation Points: 10
Solved Threads: 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.

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 
<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.

jijo cleetus
Newbie Poster
16 posts since Jan 2011
Reputation Points: 10
Solved Threads: 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?

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 
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.

jijo cleetus
Newbie Poster
16 posts since Jan 2011
Reputation Points: 10
Solved Threads: 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.

jd31068
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: