Skip to content

Instantly share code, notes, and snippets.

@praveenpuglia
Created August 24, 2013 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praveenpuglia/6329981 to your computer and use it in GitHub Desktop.
Save praveenpuglia/6329981 to your computer and use it in GitHub Desktop.
localStorage storage event example
<!DOCTYPE html>
<html>
<head>
<title>Local Storage</title>
</head>
<body>
<form action="">
<input name="comment" id="comment"></input>
<input type="submit">
</form>
<script src = "http://modernizr.com/downloads/modernizr-latest.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
//detect the support for local storage
if(Modernizr.localstorage)
{
//get the name field.
var commentBox = $('#comment');
//if local storage value is there, assign the value to it or set it to null
if(localStorage.comment)
commentBox.val(localStorage.comment);
else
commentBox.val("");
//on the blur event of the name field, save the value to local storage.
commentBox.on('blur',function(){
localStorage.setItem('comment',commentBox.val());
});
//attach an event listener to the window object for storage event.
$(window).on('storage',function(){
commentBox.val(localStorage.comment);
});
}
</script>
<script>
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment