Skip to content

Commit

Permalink
Detect whether storage is disabled by attempting to store and retriev…
Browse files Browse the repository at this point in the history
…e a value. It is the correct way to define disabledeness, and will catch cases such as localStorage throwing in Safari under Private Browsing mode.
  • Loading branch information
marcuswestin committed Mar 4, 2011
1 parent efbd223 commit a7b104a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
11 changes: 9 additions & 2 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var store = (function(){
doc = win.document,
localStorageName = 'localStorage',
globalStorageName = 'globalStorage',
namespace = '__storejs__',
storage

api.disabled = false
Expand Down Expand Up @@ -109,9 +110,15 @@ var store = (function(){
}
storage.save(localStorageName)
})
} else {
}

try {
api.set(namespace, namespace)
if (api.get(namespace) != namespace) { api.disabled = true }
api.remove(namespace)
} catch(e) {
api.disabled = true
}

return api
})();
1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
function assert(truthy, msg) {
if (!truthy) {
outputError((isSecondPass ? 'second' : 'first') + ' pass bad assert: ' + msg);
if (store.disabled) { outputError('<br>Note that store.disabled == true') }
testFailed = true
}
}
Expand Down

0 comments on commit a7b104a

Please sign in to comment.