Created
July 28, 2014 20:49
-
-
Save jasonsanjose/3423dceec63b10e0c410 to your computer and use it in GitHub Desktop.
cookie shim with localStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cookieVal = document.cookie, | |
testVal = "mixpanel", | |
localStorageKey = "mixpanel-cookie", | |
canWriteCookie = false; | |
// Test cookie | |
document.cookie = testVal; | |
canWriteCookies = document.cookie === testVal; | |
// Restore cookie | |
document.cookie = cookieVal; | |
if (!canWriteCookie) { | |
Object.defineProperty( | |
document, | |
"cookie", | |
{ | |
get: function () { | |
return window.localStorage.getItem(localStorageKey); | |
}, | |
set: function (val) { | |
window.localStorage.setItem(localStorageKey, val); | |
} | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment