onBeforeUnload and Submit() together?

Howdy folks, I’ve got a problem.

I have written a web application in .ASP with VBScript and JavaScript running  against a SQL back end. Everything works great except my BeforeUnload() event:

<%'Add a script to save any unsaved data before the client leaves the page%>
<script language="JavaScript">
window.onbeforeunload = confirmExit;

  function confirmExit()
  {if (document.Client_Info_Page.Value_Changed.value == "true")
    {if (confirm("This record has been changed. Press [OK] to save; [Cancel] to discard."))
		{document.Client_Info_Page.submit();
                 alert("Changed and save");}
    else
		{alert("Changed and no save");}
    }
  }
</script>
<%'END Add a script to save any unsaved data before the client leaves the page%>

This code checks a variable to see if anything’s been changed on the page and if it has, it asks the user if they’d like to save before exiting. The alert boxes are just to show the If/Then is working correctly (which it is) but the .Submit() function just gets ignored as the page exits.

Just to play, I made a java button and cut and pasted the submit() into and when that’s clicked it submits with no problem.

Anyone know how I can get the onBeforeUnload to push a .Submit()? I’ve changed it to stop the exit and allow the user to save, but it would be so much smoother if it could be automatic.

Thanks in advance for your wisdom!

Anyone?

OK then, does anyone have any other methods they like for catching user’s data if they try to leave a page without saving? I’m open to any suggestion here.

Thanks!

Hmm what happens if you remove the confirm call and just submit? The document might be in the process of unloading while the client answers the confirm.

If that does nothing, try alerting document.Client_Info_Page to see if it still exists.

No submit works in that beforeunload function, even when it’s by itself.

I’m not sure what you mean by “alerting document.Client_Info_Page to see if it still exists” but I have sent up alert boxes with element values and they pop up with the form data, so it seems to still exist.

so why can’t I submit() it?

The problem might be that there’s not enough time for the form to submit. The unload has to be delayed somehow. I’m not sure how to do it.

I just looked at the definition of onbeforeunload and it’s weird: http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onbeforeunload.asp

More info: http://www.webreference.com/dhtml/diner/beforeunload/bunload2.html

I can’t figure out a way to delay unload with this.

Well, I have this code in place currently:


<script language="JavaScript">
window.onbeforeunload = confirmExit;
window.onload = funct_Check_Other_Type;

  function confirmExit()
  {
  var lv_Message = "This record has been changed.";
	
  if (document.Client_Info_Page.Value_Changed.value == "true")
	
	{return lv_Message;}
  }
</script>

and this allows the user to stay on the page to manually save, but I’d like to find out how to save it for them.

If it returns FALSE (they press cancel) out of that pop-up, it exits out of the function. Any advice on how to work around that and call a Submit() or run another function only if they have trieed to leave and canceled out?

Did you try submitting the form in that function?

I changed it to this:


  function confirmExit()
  {
	
  if (document.Client_Info_Page.Value_Changed.value == "true")
	
	{alert("here we go");document.Client_Info_Page.submit();}
	
  }

</script>

and I get the alert box, but no submit.

Does anyone have any ideas to fire another event after this if they hit cancel (returns FALSE) from that alert box?

I have an idea to set a variable when they enter the script and have another script fire after this one (if they’re still on the page) and if that variable is set then it could submit.

Problem is, I can’t think on an event handler that could catch that without having to run it far more often that is necessary (like a body.onFocus() event).

Any input is appreciated!

Wait, try putting the alert after the form submit. If the form is submitting at all, that should give enough time for it to submit.

I have tried it both ways and with the submit by itself.

This:


window.onbeforeunload = confirmExit;

  function confirmExit()
  {
	
  if (document.Client_Info_Page.Value_Changed.value == "true")
	
	{document.Client_Info_Page.submit();alert("did it go?")}
	
  }

pops the alert box, but doesn’t submit. The submit by itself just goes right on to whatever they did to leave the page without doing anything. I’m getting no errors, it’s just ignoring the submit()

If the form unloaded would there be some way to allow the return false (which ends the function) to stop the unload then run a submit without having code that would fire too often or cause problems with normal use of the page?

But if the form was already unloaded, I wouldn’t be able to do this:


  if (document.Client_Info_Page.Value_Changed.value == "true")

which it does evaluate correctly from a hidden form element.

There’s got to be an elegant answer to this, but it’s eluding me…

Try using a setTimeout() to submit the form.

Hmmmm,

Tried the setTimeout() bit with a .5 second doc.form.submit() and it looks like it works, the timer doesn’t init until you press a button on the pop-up and if you OK it unloads the page before it counts down and if you cancel it then it runs half a second later.

Thank you so much!