Skip to content

Instantly share code, notes, and snippets.

@rajasika
Created June 14, 2012 15:36
Show Gist options
  • Save rajasika/2931035 to your computer and use it in GitHub Desktop.
Save rajasika/2931035 to your computer and use it in GitHub Desktop.
Local Storage Example 1
function localStack(name){
var o = new Object();
o.name = name;
// initialize index if it doesn't exist
if(!localStorage[o.name]) localStorage[o.name] = 0;
// push an item onto stack
o.push = function(data){
// get next index
var newKey = o.name+localStorage[o.name].toString();
// place item in local storage key named with index
localStorage.setItem(newKey, data);
localStorage[o.name]++;
}
// pop item off of stack
o.pop = function(){
// nothing to pop
if(localStorage[o.name] == 0) return false;
// get the last key
var popKey = o.name+localStorage[o.name].toString();
localStorage[o.name]--;
// get the last data item inserted
var data = localStorage.getItem(popKey);
// remove from local storage
localStorage.removeItem(popKey);
return data;
}
return o;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment