Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onunload

Status
Not open for further replies.

wenche1

Programmer
Sep 17, 2006
4
NO
Hi Guys

I am hoping to find a solution where I can return false from an onunload when data saved with ajax and the save cause an error.

I have a var called Zchanged which is checked in the onunload and if 1 it prompt for save with ok/cancel. This works fine. When save is started it sets a new var called Zonserver=1. What I hope to do is to wait in onunload until the Zonserver=0 and then check a new var (Zerror) to see if any error occured during save. If error, return false else return true.
Is this possible?
Thanks in advance
 
Although, I did a little test to see if you could submit your form from the unload event and it worked.
Code:
<body onunload="if(confirm('Do you want to save your changes?')) document.forms[0].submit()">
<form>
<input type="text" name="t" value="some value">
</form>

I put that code in a web page and then tried to go somewhere else. If I clicked OK, it submitted the form instead of going to where I wanted. So I guess it is possible.

One weird thing though. I tried going to the url from the page with the code above and I got a JavaScript error on the gotdotnet page. Even though I wan't on that page yet, it started executing the JavaScript there. Weird.

Adam
 
Hi Adam

Thanks for reply. It works like that on my side too, but since I use Ajax (I guess) it also walks to the right URL after save. This is fine as long as the save can be done without validating on the server. The problem is when server return something wrong, then it is just ignored since it is already on a different page and record is not saved. Maybe there is no way to cancel in that case.

Thanks anyway
Wenche1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top