Skip to content

Instantly share code, notes, and snippets.

@nicocedron
Created May 20, 2015 18:32
Show Gist options
  • Save nicocedron/004dd723a464893e5390 to your computer and use it in GitHub Desktop.
Save nicocedron/004dd723a464893e5390 to your computer and use it in GitHub Desktop.
Use localStorage in Incognito mode using UnderScore, jQuery Cookie.
hasStorage = ->
try
mod = new Date
localStorage.setItem mod, mod.toString()
result = localStorage.getItem(mod) == mod.toString()
localStorage.removeItem mod
return result
if !hasStorage()
window.CustomStorage =
length: 0
setItem: (item, val)->
@[item] = val
$.cookie(item, val);
@length+= 1
getItem: (item)->
@[item]
removeItem: (item)->
delete @[item]
$.removeCookie(item);
@length-= 1
window.CustomStorage = _.extend(window.CustomStorage, $.cookie());
window.CustomStorage.length = Object.keys(window.CustomStorage).length - 4
window.CustomSessionStorage = {}
else
window.CustomStorage = localStorage;
window.CustomSessionStorage = sessionStorage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment