Link to home
Start Free TrialLog in
Avatar of tbaseflug
tbaseflugFlag for United States of America

asked on

OnUnload - Confirm - Redirect upon choice

I wanted to set a confirmation popup based upon an  onUnload event - additionally, based upon what they select - "OK" , "Cancel" - I want to the user to be directed to one page or another -

Is this possible?
Avatar of cubrovic
cubrovic
Flag of Serbia image

something like this ?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
<!--
function whereTo() {
   var c = confirm("Are you want to go topage1.html")
  if (c) window.open("page1.htm")
  else window.open("page2.htm")
     return false
}
// -->
</script>
</head>
<body onunload="return whereTo()">
</body>
</html>




Avatar of tbaseflug

ASKER

Thanks cubrovic  -

Works great - one question for you though - I have a form on the same page and when I submit the form, the onUnload event fires as well - I guess I should have been more clear, in that I am trying to set the onUnload event when the user attempts to close the window - whereby they get a message that asks for confirmation (and based upon "ok" or "cancel" selection) they will be redirected to a new window - your script works great with this, but if they hit "cancel" and instead submit the form on the page, it still fires?
ASKER CERTIFIED SOLUTION
Avatar of cubrovic
cubrovic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MatrixDud
MatrixDud

Create a variable in the top of your html and set it when you click the submit button or any other button or link you don't want your unload script to work on.

Try this

IN YOUR HEAD

<script>
var runUnload = true;

function yourUnloadFunction()
{
   if( runUnload )
   {
      ... your code ...
   }
}
</script>

...

<input type="submit" value="submit" onClick="runUnload = false;">


That should do it.
Maybe this will work for u?

<script>
window.onbeforeunload=a;
function a()
{
 return "are you sure you want to exit without answering?";
}
</script>
I certainly appreciate all of the help on this one and have it nailed down now -

I am awaring points to cubrovic, as his was the solution that I used first - but feel ba in not giving at leats partial credit to Dword, as without his help I would not have gotten to where I am with the script - as a whole - thanks to all!!!