localstorage在safri下的坑

在ios10.2中的safri浏览器里无痕模式会导致localstorage不能正常使用

具体解决办法如下:

if (typeof localStorage === 'object') {
    try {
        localStorage.setItem('localStorage', 1);
        localStorage.removeItem('localStorage');
    } catch (e) {
        Storage.prototype._setItem = Storage.prototype.setItem;
        Storage.prototype.setItem = function() {};
        alert('Your web browser does not support storing settings locally. In Safari, the most common cause of this is using "Private Browsing Mode". Some settings may not save or some features may not work properly for you.');
    }
}

后来点击右下角小框框发现有个无痕浏览的模式。MD,关闭后一切正常

posted on 2018-01-11 15:09  杨龙飞  阅读(234)  评论(0编辑  收藏  举报

导航

Fork me on GitHub