I need to be able to run HTML forms offline. I mean they have to work without direct connection to the web server.
In an application I wrote over 5 years ago I did it by implementing a custom protocol 开发者_JAVA技巧handler - when a user initiated form submit the resulting HTTP request was recorded locally. At later time when a connection to the server becomes available a synchronization program loops through collected requests and submits them to the server collects the responses and again saves them locally for later use.
That was then. Now another customer approached me with a very similar request. What technology do you guys think I should use today?
Support for HTML5 is very limited just yet. Google gears? Or should I go back and continue using the protocol handler and custom synchronizer?
I'd say go for HTML5. Not all browsers support it; but all will. In the meanwhile, i think it's better to say "to get offline features try such or such browser", instead of "please download this huge plugin with lots of scary warnings".
Also a simple demographics: HTML5 is in what, 5% of all browsers? 10%? still a lot more than the 0% of users with Gears already installed.
It's a real pity, thanks a lot Google for pushing the envelope with Gears; but in the wild the only plugin generally accepted is Flash. Fortunately, HTML5 is almost there already, with nearly the same features.
Google Gears is a very good candidate - as an example, Google recently just announced that users would be able to attach files to emails while 'offline'. Another method might be to store the posted form data in a cookie, and when the user is able to get back online the server picks up the cookie and data with it.
Rumors are running around that Google Gears is no longer under development. When you look at the gears features and issue tracker at http://code.google.com/p/gears/issues/list?can=2&q=&sort=version&colspec=Version%20Milestone%20Owner%20ID%20Summary%20Component for the new Gears development, there are only 3 items which will be fixed in version 0.6. Also, according to this Wikipedia article (http://en.wikipedia.org/wiki/Google_Gears), it seems that Google is not doing any development, and the open source project does not have a lot of steam either. Taken from the article is this:
"In late November 2009, numerous online news sources reported that Google is going to migrate to use HTML 5 rather than Gears in the future. A Google spokesman later clarified that Google will however continue to support Gears to not break sites using it."
In other words, Gears has been deprecated. Use HTML 5 instead.
Dojo.Storage/ Dojo Offline , has a flash bridge that will enable you to store data using flash's data store. The limit is user set, but starts at 5Mb. The library component is an abstraction, and also supports HTML 5, cookies, and gears.
Plus gears will allow you to store binary blobs, if memory serves.
Google Gears is dead: http://gearsblog.blogspot.com/2011/03/stopping-gears.html
Your best bet is probably a combination of the HTML5 standards for applicationCache and localStorage.
However, applicationCache should be used sparingly - it is a real pain. This article explains why, as well as giving some pointers on how to use a hybrid approach of applicationCache and localStorage for offline VIEWING (not submission) of pages:
http://www.alistapart.com/articles/application-cache-is-a-douchebag/
Here is an article which has an example of capturing form data into localStorage, but unfortunately it doesn't provide an example of actually submitting the offline data:
http://miniapps.co.uk/blog/post/using-html5-localstorage-as-a-fallback-for-offline-form-submission
Also, beware of window.navigator.onLine to detect offline status - it is not implemented consistently across browsers, and you're probably better off having the user explicitly tell you whether they're offline or not.
-- Chad
精彩评论