Created
August 24, 2013 19:31
-
-
Save praveenpuglia/6329981 to your computer and use it in GitHub Desktop.
localStorage storage event example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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