diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index 4ef8349fa9ce9..e10e330402f5e 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -26,8 +26,26 @@ function onEach(arr, func) { return false; } +function usableLocalStorage() { + // Check if the browser supports localStorage at all: + if (typeof(Storage) === "undefined") { + return false; + } + // Check if we can access it; this access will fail if the browser + // preferences deny access to localStorage, e.g., to prevent storage of + // "cookies" (or cookie-likes, as is the case here). + try { + window.localStorage; + } catch(err) { + // Storage is supported, but browser preferences deny access to it. + return false; + } + + return true; +} + function updateLocalStorage(name, value) { - if (typeof(Storage) !== "undefined") { + if (usableLocalStorage()) { localStorage[name] = value; } else { // No Web Storage support so we do nothing @@ -35,7 +53,7 @@ function updateLocalStorage(name, value) { } function getCurrentValue(name) { - if (typeof(Storage) !== "undefined" && localStorage[name] !== undefined) { + if (usableLocalStorage() && localStorage[name] !== undefined) { return localStorage[name]; } return null;