不要讓的localStorage/sessionStorage的setItem拋出錯誤在Safari隱私瀏覽模式
看一看這樣的: https://gist.github.com/philfreo/68ea3cd980d72383c951
// Safari, in Private Browsing Mode, looks like it supports localStorage but all calls to setItem
// throw QuotaExceededError. We're going to detect this and just silently drop any calls to setItem
// to avoid the entire page breaking, without having to do a check at each usage of Storage.
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.');
}
}
Safari瀏覽器隱私瀏覽是衆所周知的原因這樣的問題。解決這個問題的最簡單方法是改變localStorage函數。
試試這個
function isLocalStorageNameSupported()
{
var testKey = 'theTestKey', storage = window.sessionStorage;
try
{
storage.setItem(testKey, '1');
storage.removeItem(testKey);
return localStorageName in win && win[localStorageName];
}
catch (error)
{
return false;
}
}
你可以找到一個詳細的文檔和其他在這裏的解決方案:https://github.com/marcuswestin/store.js/issues/42
的問題是谷歌JS裏面,這不是正確地加載和谷歌登入不起作用。如果localStorage不可用,我解決了不加載JS的問題 – GUL
@GUL如果您......您對Google Sign-In沒有使用Google JS API做了什麼?如果你在Git上有你的代碼,那很好。先謝謝你。 – JackW327
對不起,但我不能顯示整個代碼,但我可以解釋我做了什麼。如果localStorage不可用,我不加載Google JS API。爲了測試localStorage是否可用,我只是嘗試寫一個值,如果它引發一個異常而不是不可用 – GUL