├── DataSaver.jquery.json ├── README.md ├── jquery.DataSaver.min.js ├── jquery.DataSaver.js └── index.html /DataSaver.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "DataSaver", 3 | "title": "jQuery DataSaver", 4 | "description": "jQuery plugin for save your data from form fields in the web storage.", 5 | "keywords": [ 6 | "data", 7 | "save", 8 | "storage", 9 | "localstorage", 10 | "form", 11 | "fields" 12 | ], 13 | "version": "0.1.2", 14 | "author": { 15 | "name": "Seleznev Alexander", 16 | "email": "absenteg@gmail.com", 17 | "url": "http://whoisabsent.ru" 18 | }, 19 | "licenses": [ 20 | { 21 | "type": "MIT", 22 | "url": "http://www.opensource.org/licenses/mit-license.php " 23 | } 24 | ], 25 | "homepage": "https://github.com/absentik/DataSaver", 26 | "docs": "https://github.com/absentik/DataSaver/wiki", 27 | "bugs": "https://github.com/absentik/DataSaver/issues", 28 | "dependencies": { 29 | "jquery": ">=1.7" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DataSaver 2 | ======= 3 | 4 | ## ABOUT 5 | The jQuery plugin allows you to save data from form fields in the web storage. 6 | 7 | This saves users from a situation when they lost all data entered into the form. This can happen due to a crash browser, disconnection, reload the page, etc. 8 | 9 | ### Browser Support 10 | Plugin is supported in Internet Explorer 8+, Firefox 3.5+, Opera 10.5+, Chrome 4.0+, and Safari 4.0+. 11 | 12 | ## USAGE 13 | DataSaver plugin uses the jQuery JavaScript library, only. So, include just these two javascript files in your header. 14 | 15 |
16 | <script src="js/jquery.js"></script> 17 | <script src="js/jquery.DataSaver.js"></script> 18 |19 | 20 | Select elements whose data you want to save. 21 | 22 |
<textarea class="saveme"> </textarea>23 | 24 | After it, call the jQuery DataSaver plugin. 25 | 26 |
$('.saveme').DataSaver();
27 |
28 | ### Options:
29 | You can pass an options object in plugin init method.
30 | * `timeout` : Automatic save interval data (in milliseconds). If zero, it is not used (Default: `0`);
31 | * `events` : List of events that cause data save. If `undefined` or `''`, it is not used (Default: `change`);
32 | * `keyUrlAttrs` : Array of properties `window.location` object for building key (Default: `["host", "pathname"]`);
33 | * `keyExtra` : Function that return a string that is used for building key (Default: `function() { return ""; }`).
34 |
35 |
36 | $('.saveme').DataSaver({
37 | timeout: 1000,
38 | events: "change keyup",
39 | keyUrlAttrs: ["host", "pathname", "search"],
40 | keyExtra: function() {
41 | return $('#linkedfield').val();
42 | }
43 | });
44 |
45 |
46 | ### Methods:
47 | You can call some methods. Just pass their name.
48 | * `save` : Save the data in web storage;
49 | * `load` : Load the data from web storage;
50 | * `remove` : Remove the data from web storage;
51 | * `stop` : Stop the DataSaver.
52 |
53 | $('.saveme').DataSaver('remove');
54 |
55 | ### Events:
56 | You can listen DataSaver events.
57 | * `DataSaver_start`
58 | * `DataSaver_stop`
59 | * `DataSaver_save`
60 | * `DataSaver_load`
61 | * `DataSaver_remove`
62 |
63 | You can also get localStorage key (`DataSaver_key`) and value:
64 |
65 |
66 | $(document).on("DataSaver_save DataSaver_load DataSaver_remove", function(e) {
67 | console.log(e.type);
68 | console.log(e.target.DataSaver_key + " : " + localStorage[e.target.DataSaver_key]);
69 | console.log("");
70 | });
71 |
72 |
73 | ## EXAMPLE
74 | [View example](http://htmlpreview.github.io/?https://github.com/absentik/DataSaver/blob/master/index.html#example_form)
75 |
--------------------------------------------------------------------------------
/jquery.DataSaver.min.js:
--------------------------------------------------------------------------------
1 | /*
2 | * jQuery DataSaver plugin 0.1.2
3 | * https://github.com/absentik/DataSaver
4 | *
5 | * Author: Seleznev Alexander (ABSENT)
6 | * Email: absenteg@gmail.com
7 | * Website: http://whoisabsent.ru
8 | *
9 | * Licensed under the MIT license.
10 | * http://www.opensource.org/licenses/mit-license.php
11 | */
12 |
13 | ;(function(b,e,g,h){function d(a,c){this.element=a;this._defaults=f;this._name="DataSaver";this.options=b.extend({},f,c);this.action="string"===typeof c?c:"default";this.getkey();this.init()}var f={timeout:0,events:"change",keyUrlAttrs:["host","pathname"],keyExtra:function(){return""}};d.prototype.getkey=function(){var a=this.element.DataSaver_key;if("undefined"===typeof a){var c={};b.each(this.options.keyUrlAttrs,function(a,b){e.location.hasOwnProperty(b)&&(c[b]=e.location[b])});a={tagName:this.element.tagName,
14 | name:this.element.name};b(this.element).is(":input")&&(a.type=this.element.type);b(this.element).is(":radio")||(a.id=this.element.id,a.className=this.element.className);a=["DataSaver",JSON.stringify(c),JSON.stringify(a),this.options.keyExtra()].join(".");this.element.DataSaver_key=a}return a};d.prototype.load=function(){var a=this.getkey(),a=localStorage[a];if(null!=a)switch(this.element.tagName){case "INPUT":switch(b(this.element).attr("type").toUpperCase()){case "CHECKBOX":b(this.element).prop("checked",
15 | "true"===a);break;case "RADIO":b("input[type=radio][name="+this.element.name+"][value="+a+"]").prop("checked",!0);break;default:b(this.element).val(a)}break;case "SELECT":a=a.split(",");b(this.element).val(a);break;case "TEXTAREA":b(this.element).val(a)}return b(this.element).trigger("DataSaver_load")};d.prototype.save=function(){var a=this.getkey(),c;switch(this.element.tagName){case "INPUT":switch(b(this.element).attr("type").toUpperCase()){case "CHECKBOX":c=b(this.element).prop("checked");break;
16 | case "RADIO":c=b(this.element).val();break;default:c=b(this.element).val()}break;case "SELECT":c=b(this.element).val();break;case "TEXTAREA":c=b(this.element).val()}"undefined"!==typeof c&&(localStorage[a]=c);return b(this.element).trigger("DataSaver_save")};d.prototype.remove=function(){var a=this.getkey();localStorage.removeItem(a);return b(this.element).trigger("DataSaver_remove")};d.prototype.start=function(){var a=this;this.stop();this.load();"undefined"!==typeof this.options.events&&0jQuery plugin for save your data.
65 | 69 |The jQuery plugin allows you to save data from form fields in the web storage.
73 |This saves users from a situation when they lost all data entered into the form. This can happen due to a crash browser, disconnection, reload the page, etc.
74 |Plugin is supported in Internet Explorer 8+, Firefox 3.5+, Opera 10.5+, Chrome 4.0+, and Safari 4.0+.
77 |DataSaver plugin uses the jQuery JavaScript library, only. So, include just these two javascript files in your header.
80 |81 | <script src="js/jquery.js"></script> 82 | <script src="js/jquery.DataSaver.js"></script> 83 |84 |
Select elements whose data you want to save.
86 |87 | <input type="text" id="field" class="saveme" /> 88 | <textarea class="saveme"> </textarea> 89 |90 |
After it, call the jQuery DataSaver plugin.
92 |
93 | $('.saveme').DataSaver();
94 |
95 | You can pass an options object in plugin init method.
98 |
105 | $('.saveme').DataSaver({
106 | timeout: 1000,
107 | events: "change keyup",
108 | keyUrlAttrs: ["host", "pathname", "search"],
109 | keyExtra: function() {
110 | return $('#linkedfield').val();
111 | }
112 | });
113 |
114 | You can call some methods. Just pass their name.
118 |
125 | $('.saveme').DataSaver('remove');
126 |
127 | You can listen DataSaver events.
131 |You can also get localStorage key (DataSaver_key) and value:
139 |
140 | $(document).on("DataSaver_save DataSaver_load DataSaver_remove", function(e) {
141 | console.log(e.type);
142 | console.log(e.target.DataSaver_key + " : " + localStorage[e.target.DataSaver_key]);
143 | console.log("");
144 | });
145 |
146 | ABSENT, 2014
199 |