Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


OnUnload - Confirm - Redirect upon choice

Posted on 2003-02-27
Medium Priority
Last Modified: 2012-06-27
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?
Question by:tbaseflug

Expert Comment

ID: 8035995
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()">


Author Comment

ID: 8036044
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?

Accepted Solution

cubrovic earned 240 total points
ID: 8036241
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 8036977
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.

Expert Comment

ID: 8038654
Maybe this will work for u?

function a()
 return "are you sure you want to exit without answering?";

Author Comment

ID: 8042220
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!!!

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question