Skip to content

Commit

Permalink
Add try-catch-block for choosing storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Weiland committed Dec 8, 2014
1 parent 3cd6a80 commit edbac62
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ngStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
$timeout
){
// #9: Assign a placeholder object if Web Storage is unavailable to prevent breaking the entire AngularJS app
var webStorage = $window[storageType] || ($log.warn('This browser does not support Web Storage!'), {}),
var webStorage,
$storage = {
$default: function(items) {
for (var k in items) {
Expand All @@ -60,6 +60,14 @@
_last$storage,
_debounce;

try {
webStorage = $window[storageType];
webStorage.length;
} catch(e) {
$log.warn('This browser does not support Web Storage!');
webStorage = {};
}

for (var i = 0, l = webStorage.length, k; i < l; i++) {
// #8, #10: `webStorage.key(i)` may be an empty string (or throw an exception in IE9 if `webStorage` is empty)
(k = webStorage.key(i)) && 'ngStorage-' === k.slice(0, 10) && ($storage[k.slice(10)] = angular.fromJson(webStorage.getItem(k)));
Expand Down

0 comments on commit edbac62

Please sign in to comment.