Skip to content

Instantly share code, notes, and snippets.

@devdays
Created December 2, 2014 17:10
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 devdays/dad75a9fd7be69f14d09 to your computer and use it in GitHub Desktop.
Save devdays/dad75a9fd7be69f14d09 to your computer and use it in GitHub Desktop.
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