-
-
Save lukebussey/e54e073e110c9e9b9a0ca9fc60b86a78 to your computer and use it in GitHub Desktop.
cookie shim with localStorage
This file contains 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