I was working on this last year, then had a computer melt down that took me WAY off course! So I didn't even touch it again until this week. And now I don't know what I was thinking.
I'm working on a feature to autosave a form that the user is completing, so if they leave the page and come back then all won't be lost. Here's the part I'm working with:
1. var getObj, now, comment, saveName = location.pathname;
2.
3. // getCookie() is a function defined elsewhere, it just gets the value of the cookie
4. var storage = getCookie('storage'),
5. cookie_user = getCookie('username');
6.
7. if (typeof(Storage) !== 'undefined') {
8. if (storage) {
9. getObj = JSON.parse(localStorage.getItem(saveName));
10. now = new Date().getDate() + storage;
11.
12. if (getObj && cookie_user == getObj.username && getObj.timestamp <= now) comment = getObj.comment;
13. else localStorage.removeItem(saveName);
14. }
15.
16. else {
17. getObj = JSON.parse(sessionStorage.getItem(saveName));
18. if (getObj && cookie_user == getObj.username) comment = getObj.comment;
19. }
20. }
At the end, getObj should look like:
getObj = {
username : csdude55
timestamp : new Date().getDate(),
price : 100
subject : foo,
comment : bar
};
But I'm getting lost on the "storage" variable. I can't find where I EVER set a cookie named storage, anywhere! It looks like I planned for it to be a number since I'm adding it to new Date().getDate() on line 10, but that's all I've got.
IIRC, the only difference between localStorage and sessionStorage is that localStorage stays forever while sessionStorage automatically deletes when they close the browser. So maybe the plan was to give the user an option on how long they want to save it (with their setting to be stored in the "storage" cookie), but if not then use sessionStorage instead?
Can you think of any other reason that I would have planned to create this cookie in the future?