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


difrad76
12-16-2005, 09:47 AM
I have to make sure that form data has been saved. In order to do that I need to force users to populate the form.

I thought to use onBeforeUnload event to do just that. So if the form is being closed it would fire a validateForm() function. Depending on the returning value I want either just close the window or display alert about errors and abort closing window.

When I do that the onBeforeUnload event throws a dialog box where user can confirm closing.

Is there any way to avoid this dialog box all together and just act on what the validateForm() functions returns ?

This is my code


function validateForm()
{
var date1=document.getElementById("date_month").value;
var hour1=document.getElementById("hour").value;
var min1=document.getElementById("min").value;
var ampm=document.getElementById("ampm").value;
var error="";

if (date1=="")
error+="Please select a date !\n";
if (hour1=="")
error+="Please select a hour !\n";
if (min1=="")
error+="Please select a minutes !\n";
if (ampm=="")
error+="Please select a AM/PM identifier !\n";
if (error!="") {
alert (error);
return false;
} else {
window.close();
return true;
}
}

<body onbeforeunload ="return validateForm()">

Thanks

Kor
12-16-2005, 10:41 AM
You can not stop the user to close a page if he wants to by all means. All you can do is to anounce him that it would be better to complete the form before he leaves the page. Make your error variable a global one ond use this piece of code:


window.onbeforeunload = function (evt) {
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = error;
}
return error;
}

felgall
12-16-2005, 02:47 PM
That code will only work on Internet Explorer as onbeforeunload is not a real web event (only an IE event).

Ultimater
12-16-2005, 02:58 PM
That code will only work on Internet Explorer as onbeforeunload is not a real web event (only an IE event).
Actually the onbeforeunload event handler is understood in Firefox:

<script type="text/javascript">
onunload=function(){
alert("have a nice day! #2")
}
onbeforeunload=function(){
alert("have a nice day! #1")
}
</script>

Just in Firefox you need to do a little extra to grab the event object. If you tried running Kor's code and defined the varaible "error", you'll notice it actually works in Firefox as well.

Kor
12-19-2005, 02:58 AM
Yes, unbeforeunload works in FF as well. On the other hand, in this case the code tries only to prevent, at client-side level, some incomplete form inputs (to ease the server-side application from too much validation). It is not an essential code (I presume the construction will work with or without it). Even if it will works in IE only, will work for at least 2/3 of the users, so that at least in 2 cases from 3 the server-side application will be relieved from extra validation job. It would have worth to use it even if IE only

godfather
12-19-2005, 03:39 AM
hmmmmmmmmmmmm tell me how2 you get no m8

Kor
12-19-2005, 03:44 AM
? that means?