Skip to content

Instantly share code, notes, and snippets.

@d3nj3ll
Created January 19, 2012 13:50
Show Gist options
  • Save d3nj3ll/1640127 to your computer and use it in GitHub Desktop.
Save d3nj3ll/1640127 to your computer and use it in GitHub Desktop.
Client-side Autosave Using localStorage with jQuery in textarea
$(document).ready(function() {
///////////////////////
//
// Recovery Below
//
///////////////////////
// Retrieve the object from storage onReady
var autosave = localStorage.getItem('file');
// parses the string (btw. its UTF-8)
var text = JSON.parse(autosave);
//modifies the textarea with the id="inputTextArea"
$("textarea#inputTextArea").val(text);
////////////////////////
//
// Autosaver below
//
////////////////////////
// Autosave on keystroke works in offline mode
$("textarea#inputTextArea").change (function(){
// pulls the value from the textarea
var file = $('textarea#inputTextArea').val();
// sets the file string to hold the data
localStorage.setItem('file', JSON.stringify(file));
});
});
@d3nj3ll
Copy link
Author

d3nj3ll commented Jan 19, 2012



AUTOSAVE.JS



I have been working on devving server-based editors of various sorts for my jailbroken iPad, and one of the issues I continuously faced was that when I switched apps or even between tabs in safari, that the page would reload. That can really suck if you don't save in between!!! So I read up on localStorage and wrote this quick script. It is also VERY fast because it saves locally.

You might want to use this for other things too, such as avoiding throwing an error if the string doesn't exist onReady. Go ahead.



@esromneb
Copy link

This plugin is a much simpler way to accomplish what this Gist does. Not dissing just helping out others who may stumble here! https://github.com/jas-/jQuery.handleStorage/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment