/**
* offlineForm
*
* @version 0.1.0
* @example $('form.offline').offlineForm();
* @author Brewal RENAULT http://oziolab.fr/
*
* This is a jQuery plugin for synchronizing forms when navigator is
* offline. If it is, forms will be stored into localStorage, until an online
* event is detected. If it is not, forms are directly sent without any other
* action done.
*/
(function($)
{
var defaults =
{
// localStorage key of the plugin
key: "offlineForm",
// class css
classname: "offlineForm",
// if true, send the form with ajax
onlineAjaxSend: false,
// called before syncing all local data
beforeSync: null,
// called when forms are synchronized
afterSync: null,
// called when a form is syncrhonized
onSync: null,
// called when offline and a form is submited
onStorage: null,
// called when an error occured during syncing
onError: null,
// called before sending the ajax request when online
beforeOnlineAjaxSend: null,
// callback for online ajax sending
onlineAjaxCallback: null
};
var p = defaults;
function onFormSubmit() {
form = $(this);
// if navigator is online, we just send the form
if (navigator.onLine) {
if (p.onlineAjaxSend) {
if (p.beforeOnlineAjaxSend) {
p.beforeOnlineAjaxSend();
}
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function(ret) {
if (p.onlineAjaxCallback) {
p.onlineAjaxCallback(ret);
}
}
}).fail(function(e) {
if (p.onError) p.onError(e);
return false;
});
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.