...

View Full Version : Confirm closing window and run script on close.



Kevin Ar18
07-24-2002, 07:07 PM
I have been using the following script to ask the user if they wanted to close the window. So far it has worked great.


<script language="JavaScript" event="onbeforeunload()" for="window">
if(mapSaved) {event.returnValue = "Exit the Map Editor?";}
else {event.returnValue = "You have not saved your map.\nAre you sure you want to exit the Map Editor?";}
</script>

However, I now would like to add something in. If the user decides to leave, I would like to run a script (aka submit a form) just before it closes.

Can anyone come up with a way to do this and still have that confirmation message come up?

I tried the following code, but it didn't submit the form and also gave me the confirmation message twice:

<script language="JavaScript" event="onunload()" for="window">
document.closeserver.submit();
</script>

KrazyKid
07-24-2002, 07:10 PM
onbeforeunload (http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/events/onbeforeunload.asp)

Kevin Ar18
07-24-2002, 07:26 PM
Thanks for the info.
From reading it, I didn't find any ways to do exactly what I needed.

However, I got close.
Here's what I tried (problem is it submits the form whether they chose OK or Cancel.


<script language="JavaScript" event="onbeforeunload()" for="window">
if(mapSaved) {event.returnValue = "Exit the Map Editor?";}
else {event.returnValue = "You have not saved your map.\nAre you sure you want to exit the Map Editor?";}
document.closeserver.submit();
</script>


So, any ideas?

BTW, thanks for the URL.

KrazyKid
07-24-2002, 07:30 PM
Here is an idea...

You could set a flag in the onbeforeunload

based on that flag value decide whether or not to call the routine for the form submission.

Hope that helps!

Kevin Ar18
07-24-2002, 07:31 PM
What do you mean by setting a flag?

Kevin Ar18
07-24-2002, 07:34 PM
Ok, found out what a flag was. We seem to both be on the same track. I was just trying to do that. However, I don't know how to get a value from the confirm message based on whether they chose OK, or Cancel and there lies the problem. So I guess the questions is, how can I determine if they chose OK or if they chose Cancel?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum