Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukebussey/e54e073e110c9e9b9a0ca9fc60b86a78 to your computer and use it in GitHub Desktop.
Save lukebussey/e54e073e110c9e9b9a0ca9fc60b86a78 to your computer and use it in GitHub Desktop.
cookie shim with localStorage
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