/
jquery.formSavior.js
64 lines (54 loc) · 2.36 KB
/
jquery.formSavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(function ($, window, document) {
"use strict";
$.fn.formSavior = function (options) {
return this.each(function () {
var cfg = {
'msg' : 'There are unsaved changes on this form',
'noprompt' : 'fs_noprompt'
};
if (options) {
$.extend(cfg, options);
}
var originals = '',
showalert = true,
$form = $(this),
$win = $(window),
$doc = $(document);
// Adding a click listener for noprompt links, onclick adds a noprompt class to the form
$doc.on('click', '.' + cfg.noprompt, function () {
$(this).closest("form").addClass(cfg.noprompt);
});
// When multiple forms are on the page, and one of the forms triggers the beforeunload
// alert, reset showalert back to true
$win.bind('fs_beforeUnloadTriggered', function () {
showalert = true;
});
function extractFormData() {
var formdata = $form.serialize();
$form.find('input[type=file]').each(function () {
formdata = formdata + $(this).val();
});
return formdata;
}
function saveOriginals() {
originals = extractFormData();
}
function allowSave() {
showalert = false;
}
function savePrompt() {
var current = extractFormData();
if (current !== originals &&
showalert === true &&
!$form.hasClass(cfg.noprompt)) {
$win.trigger('fs_beforeUnloadTriggered');
return cfg.msg; // The beforeunload event takes
// only a string as argument and displays the prompt.
}
}
$doc.ready(saveOriginals); // Saving original state of the form once the document is loaded
$form.submit(allowSave); // Allowing form to save when it is submitted
$win.bind('beforeunload', savePrompt); // Function call here to check if the form has changed
});
};
})(jQuery, window, document);