views:

3892

answers:

5

I have a page which does quite a bit of work and I don't want the user to be able to navigate away from that page (close browser, hit back button, etc.) without getting a warning. I found that the onbeforeunload event (which I think is IE-specific, which works fine for me as the project uses lots of ActiveX) works great.

Problem is, I want the user to be able to click on a little "help" icon in the upper-right corner and pop up a help window at any time. This causes onbeforeunload to fire, even though the main window never goes anywhere and the page never unloads.

The JavaScript function that runs when the onbeforeunload event runs just puts text into event.returnValue. If I could ascertain, somehow, that the help icon is the one that was clicked then I could just not put text into event.returnValue in that situation. But how could I have the page figure that out?

+2  A: 

EDIT: My "workaround" below is complete overkill, based on my lack of understanding. Go with Shog9's answer above.

OK so while I was writing the question, I came up with a workaround which will work for now.

I put a global JavaScript variable in act as a boolean on whether or not the icon is being hovered over. Then, I attach events to the image's onmouseover and onmouseout events and write functions that will set this value. Finally, I just code in the function that handles onbeforeunload that will check this value before setting event.returnValue.

Probably not a flawless workaround but it will work for now.

Schnapple
+6  A: 

Let me guess: the help "icon" is actually a link with a javascript: url? Change it to a real button, a real link, or at least put the functionality in an onclick event handler (that prevents the default behavior). Problem solved.

<!-- clicking this link will do nothing. No onbeforeunload handler triggered. 
Nothing. 
And you could put something in before the return false bit...
...and the onunload handler would still not get called... -->
<a href="http://www.google.com/" onclick="return false;">blah1</a>
<!-- this should also do nothing, but IE will trigger the onbeforeunload 
handler -->
<a href="javascript:void(0)">blah2</a>
Shog9
Actually it was a real link - I was on the mistaken impression that onclick fired after onbeforeunload. Thanks.
Schnapple
A: 

I have a method that is a bit clunky but it will work in most instances.

Create a "Holding" popup page containing a FRAMESET with one, 100% single FRAME and place the normal onUnload and onbeforeUnload event handlers in the HEAD.

<html>
<head>
<script language="Javascript" type="text/javascript">
  window.onbeforeunload = exitCheck;
  window.onunload = onCloseDoSomething;

  function onCloseDoSomething()
  {
  alert("This is executed at unload");
  }

  function exitCheck(evt)
  {
  return "Any string here."}
  </script>
  </head>

    <frameset rows="100%">
    <FRAME name="main" src="http://www.yourDomain.com/yourActualPage.aspx"&gt;
    </frameset>
<body>
</body>
</html>

Using this method you are free to use the actual page you want to see, post back and click hyperlinks without the outer frame onUnload or onbeforeUnload event being fired.

If the outer frame is refreshed or actually closed the events will fire.

Like i said, not full-proof but will get round the firing of the event on every click or postback.

Kevin Dark
A: 

on the internet you will find many people suggesting you use something like

window.onbeforeunload = null

but this does not work for me in IE6. reading up in the MSDN docs for the event object i found a reference to the event.cancelBubble property, which i thought was the solution. but thanks to Orso who pointed out that setting "event.cancelBubble=true" is useless, the way to get rid of the confirm prompt is to exclude the return statement altogether, i chose to use a boolean variable as a flag to decide whether to return something or not. in the example below i add the javascript code programattically in the code behind:

Page.ClientScript.RegisterStartupScript(typeof(String), "ConfirmClose", @" <script>  window.onbeforeunload = confirmExit;  function confirmExit()  {   if(postback == false)    return ""Please don't leave this page without clicking the 'Save Changes' or 'Discard Changes' buttons."";              }      </script>");

then the help button contains the following aspx markup:

OnClientClick="postback=true;return true;

this sets the 'postback' variable to true, which gets picked up in the confirmExit() function, having the effect of cancelling the event.

hope you find this useful. it is tested and works in IE6 and FF 1.5.0.2.

A: 

hi jake this solution is good but i have problem width time of onbeforeunload redirect to another page, this is not constant even i put setTimeout or while < 10800000 milesec, how i can stop mi script in the same time?

helpme tnaks..

mi code is:

<body onbeforeunload="return enviar();"> <a href="http://www.google.com" onclick="javascript:postback=false;return true;">#</a> </body>


function enviar(){

 if(postback==false){
 displayfadeinbox();
 pause(10800000);
 }

}


function pause(milisec){ var d = new Date(); var begin = d.getTime();

while ((d.getTime() - begin ) > milisec){ // nothing... }

}