Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Local storage save and retrieve
function SaveYourName() {
var yourName = $("#yourName").val();
if (Modernizr.localstorage) {
try {
//saves to the database, “key”, “value”
localStorage.setItem("name", yourName);
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
//data wasn’t successfully saved due to quota exceed so throw an error
alert('Quota exceeded!');
}
}
$("#yoursavedname").append("Your name was saved:" + localStorage.getItem("name"));
}
else {
var exdate = new Date();
exdate.setDate(exdate.getDate() + 30);
var c_value = escape($(yourName).val()) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
$("#yoursavedname").append("Your cookie:" + document.cookie);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment