Javascript onUnload

If I put a <body onUnload="do_unload();"> in an HTML page, can the do_unload function do ajax calls to upload data from Javascript arrays to the web server (using php scripts on the web server)?

Then when it completes, do a "return true"; and the Unload will occur, the browser remembering all the other details of the unload?

Richard KortsAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
You'll have to test it yourself since we can't know what you are going to put in your code.  I woudn't even guarantee that all the browsers will act the same.  But if you do anything that seems to prevent someone from leaving your site or even your page, it is likely they won't come back.  'onUnload' has a bad reputation for alienating visitors who are trying to go somewhere else.
Richard KortsAuthor Commented:
Dave Baldwin,

It's  a private application with a "captive" audience. I thought of this as a way to save information. I could go into all the details, but it takes too long.

I know what you mean. Like "Are you sure you want to leave this page" and all that kind of crap.
Dave BaldwinFixer of ProblemsCommented:
Just try it then.  I don't see that that would first choice though.  If they close the browser instead of going to a different page, it won't work.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Richard KortsAuthor Commented:

Here is what I'm trying to do. I welcome alternate suggestions.

We have an app that is browser based, used in the field by service techs that have iPads. It's php with a MySQL database. The iPads use Safari (could use Chrome if necessary).

Of course in the field, there is often no WiFi, so they have slow access.

I originally designed things to work by accessing database tables as need be.

For example, techs use parts in some of their jobs. One feature of the add parts is parts search; you type in any few letters, click search & it finds all parts that have those characters in their description. Of course it goes to the server and does a SQL like "SELECT * from parts where pdesc like '%xxx%'". If any found, displays the list in a dropdown, they pick what they want.

If no WiFi, techs experience too long wait times, do a refresh, things get screwed up, have to start over, get annoyed at the stupid people who built this (me).

So what I am trying is to do the ENTIRE parts thing in the browser using Javascript arrays and local storage (HTML 5). I load the entire parts table into local storage (~ 600 rows) when the tech logs in, then access it as need be, using Javascript arrays, etc.

I want to let them add & modify the parts needed for a specific Work Order WITHOUT round trips to the server. Then when done, they can click a "Done" button, select another menu option, etc.

So I'm thinking whenever they do one of those things, the page unloads, I can capture that Unload & using ajax, update the SERVER parts table for that SPECIFIC workorder with the parts added, modified or deleted in the session. There may be a pause, but they WON'T loose their work.

Your thoughts?
Dave BaldwinFixer of ProblemsCommented:
Since as you have described, they often lose their connection, even 'onunload' isn't going to send the data back to the server at times.
Richard KortsAuthor Commented:

So what would you recommend?

I'm not sure they lose the connection, I think it's more like they think it's too slow & try "something" like refresh, etc.
Dave BaldwinFixer of ProblemsCommented:
The obvious thing is an app that runs on their device and reports back to the server only when they have a connection
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.