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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more


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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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

800 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