Skip to content

Instantly share code, notes, and snippets.

@elrrrrrrr
Created August 12, 2014 13:26
Show Gist options
  • Save elrrrrrrr/bb03981afeb125d7f432 to your computer and use it in GitHub Desktop.
Save elrrrrrrr/bb03981afeb125d7f432 to your computer and use it in GitHub Desktop.
localstroge hotfix by sofish
// 解决隐私模式下 localStorage 不正常问题
;(function() {
var KEY = '_localStorage_'
, VALUE = 'test';
// 检测是否正常
try {
localStorage.setItem(KEY, VALUE);
} catch(e) {
var noop = function() {};
localStorage.__proto__ = {
setItem: noop,
getItem: noop,
removeItem: noop,
clear: noop
};
}
// 删除测试数据
if(localStorage.getItem(KEY) === VALUE) localStorage.removeItem(KEY);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment