Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Javascript onUnload

Posted on 2015-01-23
Medium Priority
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 84

Accepted Solution

Dave Baldwin earned 2000 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 84

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.
Technology Partners: 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 84

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 84

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

721 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