Solved

Javascript onUnload

Posted on 2015-01-23
7
93 Views
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?

Thanks
0
Comment
Question by:Richard Korts
  • 4
  • 3
7 Comments
 
LVL 83

Accepted Solution

by:
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.
0
 

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.
0
 
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Richard Korts
ID: 40573328
Dave,

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?
0
 
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.
0
 

Author Comment

by:Richard Korts
ID: 40573747
Dave,

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.
0
 
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now