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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

something like this ?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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
// -->
<body onunload="return whereTo()">

tbaseflugAuthor Commented:
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?
Add this function and call it from the form's onsubmit event handler

function FormSubmited() {
   window.onunload = null

<form onsubmit="FormSubmited()" ...>

That will cler the onunload event if the user submits the form

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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


var runUnload = true;

function yourUnloadFunction()
   if( runUnload )
      ... your code ...


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

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

function a()
 return "are you sure you want to exit without answering?";
tbaseflugAuthor Commented:
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!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.