Click to See Complete Forum and Search --> : Overriding onBeforeUnload dialog message


welles
February 9th, 2006, 08:51 PM
Hi there,

Does anyone know how to suppress the default messages?

1. "Are you sure you want to navigate away from this page?"
2. "Press OK to continue, or Cancel to stay on current page"

I need to provide the user a custome message. I tried many approachs, but non of them seem to work. I tried to use a confirm box instead, but for some reason, the return value for onBeforeUnload event seem to be used as string, so return false to the event CANNOT stop the page from refreshing.

Thanks,
Welles

Dr. Script
February 9th, 2006, 09:52 PM
What code are you using? I'm not sure on how to solve the problem, but I'd like to see what you are trying ...

PeejAvery
February 10th, 2006, 01:02 AM
Well, onbeforeunload is only JavaScript and VBScript. Which one are you using? What are you trying to accomplish? And why can't you not use onbeforeunload?

As Dr. Script pointed at, we need to see some code. Can you please post it?

welles
February 10th, 2006, 01:20 PM
Thanks for you guys' reply, one of my many attemp to solve this problem is to define a new function for the event but it still doesn't work.

my goal: to define a custom dialog box (that is, no other message would appear on the dialog box except the message i pass to it)





<script language="javascript">
window.onbeforeunload = onBeforeUnload_Handler;

function onBeforeUnload_Handler(){
fConfirm('param1','param2','param3','param3');
}



function fConfirm(p1,p2,p3,p4)
{

//do something with p1, p2, p3, p4....



//this shows the default dialog box
//event.returnValue = 'Any changes will be lost if you proceed.';


//this shows the custom confirm box, but it doesn't stop the page from

//refreshing even the user click the "Cancel" button
if(!confirm("Any changes will be lost if you proceed.")){
return false;
}
return true;
}
}
</script>

PeejAvery
February 10th, 2006, 01:40 PM
So you want a confirm box that will display a message you tell it to and do what? Is this supposed to run when someone tries to leave/close your page?

Remember that onbeforeunload is IE only. You can use onunload for multiple browser compatibility.

welles
February 10th, 2006, 04:51 PM
I want the confirm box to pop up when the user leaves a page with unsaved data.

Actually, that was what I thought before. But I tested onBeforeUnload on FixFox and it works. O, well, since most of my users use IE, so this is not a big problem for me.

PeejAvery
February 10th, 2006, 05:16 PM
Actually, that was what I thought before. But I tested onBeforeUnload on FixFox and it works.
That is because Firefox is an open-source multibrowser compatible internet browser. I know it is rare but Netscape (unfortunately) still exists.

And I assume you want the leave to be cancelled if he/she clicks the "Cancel" button on the confirm. This would show that he/she wants to save the unsaved data.

welles
February 10th, 2006, 06:13 PM
Exactly, but for some reason, the "Cancel" button doesn't stop the page from refreshing.

for example:

<!--This wouldn't stop the page from refreshing wether the user clikcs 'OK' or 'Cancel'-->
<page onBeforeUnload="confrim('Are you sure to leave without saving data?')">

<!--This would put "True" on the default onBeforeUnload dialog box if the user clicks "OK", and "False" if the user click "Cancel". Note, different from other event, the return value is seen as a string instead of a boolean.-->
<page onBeforeUnload="return confrim('Are you sure to leave without saving data?')">

PeejAvery
February 10th, 2006, 06:27 PM
How about something more along the lines of returning false and calling a function?

<script language="JavaScript">
function checksave(){
if(confirm("Are you sure to leave without saving data?")){
self.close();
}
}
</script>
<body onunload="return false; checksave()">

welles
February 10th, 2006, 07:20 PM
I've already tried to use unload, but it seems like that it is too late to do the checking in the unload event. The page would be refreshed whether the user chooses OK or Cancel.

PeejAvery
February 11th, 2006, 01:02 AM
I've already tried to use unload, but it seems like that it is too late to do the checking in the unload event. The page would be refreshed whether the user chooses OK or Cancel.
Then use onBeforeUnload. The following works perfectly for me.

<html>
<script language="JavaScript">
function checksave(){
if(confirm("Are you sure to leave without saving data?")){
self.close();
}
}
</script>
<body onBeforeUnload="return false; checksave()">
</body>
</html>

welles
February 14th, 2006, 12:50 AM
Thanks for your help. But are you sure you code works?

When I run you code, I only see the the default onBeforeUnload dialog box with the work "false" in the middle and never see your custom confirm box.

Like I said before, the onBeforeUnload event read the return value as a string and print it on the default onBeforeUnload dialog box.

welles
February 14th, 2006, 12:52 AM
On a second thought, you code wouldn't work event the event read the return value as boolean. Because it returns before it call checkSave(), so checkSave() would never be called.

PeejAvery
February 14th, 2006, 05:53 AM
But are you sure you code works?
Have you even tried it?

I did say...
The following works perfectly for me.

On a second thought, you code wouldn't work event the event read the return value as boolean. Because it returns before it call checkSave(), so checkSave() would never be called.
Yes, it does. The return is only on the event handle itself not the rest of the onBeforeUnload commands.

I have done this code in IE, Firefox, and Safari. It works.

rsarun
September 21st, 2007, 11:41 AM
Hi Welles,

I also want to achieve a similar behavior as you. I need to capture the user clicking on the "cancel" button in the default prompt. But am not able to do it. So, instead i tried to use my own custom message using the "confirm" function but am not able to pass this result to the onbeforeunload function without calling the default prompt again. Did you find any solution for your problem. If so, please share you solution.

Regards,
rsarun

andreasblixt
September 21st, 2007, 12:34 PM
Yes, it does. The return is only on the event handle itself not the rest of the onBeforeUnload commands.

I have done this code in IE, Firefox, and Safari. It works.

I'm not sure what you've done to manage this, but...
1) JavaScript executed in HTML event attributes is procedural just like all other JavaScript, and a return statement will cancel any following code.
2) Your code does not work. Running a return false statement in onbeforeunload followed by a call to another function will result in a question whether I want to leave the page with no custom message in Firefox 2, and "false" as the custom message in Internet Explorer 7. It doesn't work at all in other browsers because they do not support onbeforeunload.

Blocking users from leaving a page is a tricky area because it could be used in harmful ways. Firefox and Internet Explorer seem to allow the programmer to get the user to confirm leaving the page once using the onbeforeunload event, however, and here's how you use it:
<body onbeforeunload="return 'Leaving the page will result in a loss of work.'">
The browser will add some of its own text to clarify to the user what is happening.

PeejAvery
September 22nd, 2007, 07:20 PM
Welcome to the forums rsarun. Please remember that when you have a question, to create a new thread instead of posting on somebody else's problem. The thread you posted in is 1.5 years old.

skylord5816
October 29th, 2007, 06:01 PM
Im having same problems... Ive tried everything! Plz help!
P.S. Welles, youve posted on a lot of forums... have u got the answer yet?

PeejAvery
October 29th, 2007, 09:22 PM
Welcome to the forums, skylord5816. Please remember to create a new thread when you have a problem. Please don't post on somebody else's.