Javascript onUnload

Posted on 2015-01-23
Last Modified: 2015-01-30
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?

Question by:Richard Korts
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 83

Accepted Solution

Dave Baldwin earned 500 total points
ID: 40567726
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.

Author Comment

by:Richard Korts
ID: 40567778
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.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40567789
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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

by:Richard Korts
ID: 40573328

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?
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40573611
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.

Author Comment

by:Richard Korts
ID: 40573747

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.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40573879
The obvious thing is an app that runs on their device and reports back to the server only when they have a connection

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question