JavaScript - 'onbeforeunload' event - Asked By Srinivas Chundi on 20-Jul-14 12:21 AM

After the confirmation dialog of 'onbeforeunload' event is shown to the user, how to know his response to that?
I mean, is there anyway to 'catch' whether he has clicked 'OK' or 'Cancel'..?
I need this because, I need to perform something only when the user says  'Cancel'.
I appreciate ur response in this regard.

Perhaps this little diddy can help... - Asked By Robbe Morris on 01-Aug-01 02:42 PM

<SCRIPT language=JavaScript>

    function CheckRedirect()
    {
            var fRef;
            fRet = confirm('Are you sure?');
            alert(fRet);
    }

</SCRIPT>

   <a href=javascript:CheckRedirect(); >click here to test</a>

well - Asked By Heaven's Martini on 01-Aug-01 03:22 PM

thats close

but I would say

if (!confirm('are you really sure?'))
 // perform false stuff (pressed cancel)

Yeah...Yeah...Yeah - Asked By Robbe Morris on 01-Aug-01 03:25 PM

Always trying to upstage my basic examples for easy reading...Chuckle.
since I don't have original thoughts - Asked By Heaven's Martini on 20-Jul-14 12:22 AM
i have to play "one upmanship coder"

Got my new pc today.. wooo hooo.
C# -- here i come.

I was debugging this morning.
I wrote  a small popup window to run some code on exiting of a social security number field(checking against db) and the entire popup page is done with response.write 's

so i had 
response.write "<head> ..."
response.write "" & vbCrlf
//response.write " alert('hi');"

I guess there isn't enough javascript syntax in vbscript for my tastes.
Chuckle...got my new truck last night! - Asked By Robbe Morris on 20-Jul-14 12:23 AM
2001 Ford Explorer Sport Track.
oh man - Asked By Heaven's Martini on 20-Jul-14 12:23 AM
I just got a 2001 Silverado Ext Cab.
(You like that "El Camino humped an explorer"?
Love it! - Asked By Robbe Morris on 01-Aug-01 04:10 PM
.
Pl understand the problem - Asked By Srinivas Chundi on 20-Jul-14 12:24 AM
I hope I was not clear enough in explaining the problem.
In fact, the problem is with 'onbeforeunload()' event.
This is an IE specific event which is fired before
onUnload() event. This can be applied to the window by 2 ways.
1) by scripting like this...
2)second approach is by applying the event to  tag.
<SCRIPT>
function askUser(form)
{
mess = "DEBUG:: BY CHUNDI...You may lose your changes!!";
event.returnValue = mess;
}
</SCRIPT>
If U tryi one of these u'll understand what I meant by "confirmation dialog box" in my note.
Well... - Asked By Robbe Morris on 01-Aug-01 04:35 PM
This page looks like a successful attempt at performing what you want (and yes, you being clearer helped).
View the page source and away you go...

http://www.webreference.com/dhtml/diner/beforeunload/
Thanks but ..Nope... - Asked By Srinivas Chundi on 20-Jul-14 12:24 AM
but I have tried this page alrady and I did not find the desired point.
onbeforeUnload - Asked By Peter Bromberg on 01-Aug-01 05:05 PM
Anyway, now that everybody's done their "piece"  it appears to me that the default behavior of onBeforeUnload is such that if you set the event.returnValue to ANY string, you lose control at that point. There are other ways to trap this, namely by using onUnload, which does give you finer - grained control. Try coding your stuff into onUnload using a window.cofirm and trapping for the button that was pressed.
onUnload does not help...!! - Asked By Srinivas Chundi on 20-Jul-14 12:25 AM
But sir, 'onUnload' gets fired only after the page is unloaded.
So, this is not useful to perform something within the page, if the user says 'No' and wishes to remain in the same page.
The situation I have is slightly different here.
When the user is editing a page and he accidentally/incidentally clicks on any menu item that exists on left hand side frame, he should be warned. And if he says 'no', then the 'menu item' he clicked earlier should remain get highlighted. That means, we should be able to 'stop the highlighting' of new menu, until he says 'yes, I am intentionally coming out of my edit page'.
Actually the problem starts with the 'highlighting' stuff. Unfortunately, this is one of the requirements for better readability.
The other idea is disabling all other links when user 'edits' any page. But this was not accepted by our client. He wants all the links available all the time.
So, how could I get out of this problem..?
That sounds you are trying to trap - Asked By Robbe Morris on 02-Aug-01 09:31 AM
your exit from the page from YOUR links and not necessarily the browser characteristics link the BACK button or CLOSE button.

Is this true?
yes,, - Asked By Srinivas Chundi on 02-Aug-01 09:56 AM
yes..it is true.
Exit to the page could be any enabled menu item on left frame/top frame, which may even load entirely different application some times.
Ah... - Asked By Robbe Morris on 02-Aug-01 10:19 AM
Sounds like you need to create a separate JavaScript function that "all" of your links get routed through as a function call.  The function itself could handle the redirecting based on an input parameter.

Then, put your confirmation message inside that function.
I would think that would be the simplest route to go.
onUnload Sample Code - Asked By Peter Bromberg on 02-Aug-01 11:14 AM
This should do it:
<SCRIPT> 
 var blnBeenHere =false;

   function goSomewhere( sURL)
         {
		if(doMe())
		{
            window.location=sURL;
			}
		else
		{
		return false;
		}
      }

function doMe(){
if (blnBeenHere)
return false;
blnBeenHere=true;
 if(!confirm("ARE YOU SURE"))
{
alert("USER CANCELED");
// do your cancel processing here...
 return false;
}
return true;
}
</SCRIPT> 
 
  <A NAME=myLink onclick=goSomewhere("http://www.yahoo.com")><u>Click Here!</u></A>
confirm can not.. - Asked By Srinivas Chundi on 02-Aug-01 11:14 AM
confirm() can not stop the unloading of page in 'onbeforeunload' event. Though the user clicks on 'cancel', it will exit the page as usual.
['return false' will not work here].

So, that will not work...!!

Only thing that can stop unloading is, 'event.returnValue=message'.
That wasn't even close to what I suggested... - Asked By Robbe Morris on 02-Aug-01 11:16 AM
...The javascript function would NOT rely on the onbeforeunload event.  The function gets called when the user clicks the link.  Read the post closer.
it wont work... - Asked By Srinivas Chundi on 02-Aug-01 11:20 AM
all this is very fine sir.
But try clicking on some link outside ur page.
It will not work...!! If you click on 'cancel' also, it will load the new page.
??? - Asked By Srinivas Chundi on 20-Jul-14 12:27 AM
'onbeforeunload' is the only IE event that could stop unloading a page. And unless the response is trapped, my job can not be done.
What exaclty do u mean by "Read the post closer"..?
You mentioned a specific need... - Asked By Robbe Morris on 02-Aug-01 12:09 PM
...to react when someone clicked a link.  Currently, you are attempting to use onbeforeunload.  I suggested running all of your links through a single javascript function that could handle the confirm and redirect to the URL if OK is clicked.

I would think that and the combination of some of your own ideas would help you out.
it won't work? - Asked By Peter Bromberg on 20-Jul-14 12:28 AM
It worked fine for me Srinivas. You have a lot of suggestions here. When you come up with something innovative, post it for others to see.
it did not work sir.. - Asked By Srinivas Chundi on 20-Jul-14 12:29 AM
I dont know what worked for U sir..?
Did U completely understand the situation I explained..?
'onUnload' gets fired after the page is completely unloaded.
I noticed that 'onBeforeUnload' is the only I.E event that can stop the unloading of a web page.
'confirm()' method fails to stop unloading of page even though yoyu 'return false'.
Some round about way has to be found out to solve this...!!
I am working on it...I'll definitely post it once I find something.
Use this, it gives u a gd explanation - vinoth babu replied to Srinivas Chundi on 17-Nov-09 03:53 AM
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
  {
  document.write("You pressed OK!");
  }
else
  {
  document.write("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>
use confirm box - vinoth babu replied to Srinivas Chundi on 17-Nov-09 03:55 AM
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
  {
  document.write("You pressed OK!");
  }
else
  {
  document.write("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onclick="show_confirm()" value="Show a confirm box" />

</body>
</html>
srrry use it now, replace onclick by onunload k - vinoth babu replied to Srinivas Chundi on 17-Nov-09 03:57 AM
<html>
<head>
<script type="text/javascript">
function show_confirm()
{
var r=confirm("Press a button");
if (r==true)
  {
  document.write("You pressed OK!");
  }
else
  {
  document.write("You pressed Cancel!");
  }
}
</script>
</head>
<body>

<input type="button" onunload="show_confirm()" value="Show a confirm box" />

</body>
</html>