-
February 9th, 2006, 08:51 PM
#1
Overriding onBeforeUnload dialog message
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
-
February 9th, 2006, 09:52 PM
#2
Re: Overriding onBeforeUnload dialog message
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 ...
*9-11-01* Never Forget; Never Forgive; Never Relent!
"If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan
-
February 10th, 2006, 01:02 AM
#3
Re: Overriding onBeforeUnload dialog message
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?
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 10th, 2006, 01:20 PM
#4
Re: Overriding onBeforeUnload dialog message
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)
Code:
<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>
-
February 10th, 2006, 01:40 PM
#5
Re: Overriding onBeforeUnload dialog message
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 10th, 2006, 04:51 PM
#6
Re: Overriding onBeforeUnload dialog message
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.
-
February 10th, 2006, 05:16 PM
#7
Re: Overriding onBeforeUnload dialog message
Originally Posted by welles
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 10th, 2006, 06:13 PM
#8
Re: Overriding onBeforeUnload dialog message
Exactly, but for some reason, the "Cancel" button doesn't stop the page from refreshing.
for example:
Code:
<!--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?')">
-
February 10th, 2006, 06:27 PM
#9
Re: Overriding onBeforeUnload dialog message
How about something more along the lines of returning false and calling a function?
Code:
<script language="JavaScript">
function checksave(){
if(confirm("Are you sure to leave without saving data?")){
self.close();
}
}
</script>
<body onunload="return false; checksave()">
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 10th, 2006, 07:20 PM
#10
Re: Overriding onBeforeUnload dialog message
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.
-
February 11th, 2006, 01:02 AM
#11
Re: Overriding onBeforeUnload dialog message
Originally Posted by welles
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.
Code:
<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>
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
February 14th, 2006, 12:50 AM
#12
Re: Overriding onBeforeUnload dialog message
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.
-
February 14th, 2006, 12:52 AM
#13
Re: Overriding onBeforeUnload dialog message
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.
-
February 14th, 2006, 05:53 AM
#14
Re: Overriding onBeforeUnload dialog message
Originally Posted by welles
But are you sure you code works?
Have you even tried it?
I did say...
The following works perfectly for me.
Originally Posted by welles
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.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
September 21st, 2007, 10:41 AM
#15
Re: Overriding onBeforeUnload dialog message
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|