Skip to content

Commit 3eac341

Browse files
committed
Handle case if localStorage is not supported by the browser
For example, if LS is disabled by browser
1 parent 84d5fc7 commit 3eac341

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

app/scripts/ng-xdLocalStorage.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ angular.module('xdLocalStorage', [])
2929
return {
3030
init: function (options) {
3131
var defer = $q.defer();
32-
options.initCallback = function () {
33-
apiReady.resolve();
34-
defer.resolve();
32+
options.initCallback = function (localStorageSupported) {
33+
if (localStorageSupported) {
34+
apiReady.resolve();
35+
defer.resolve();
36+
} else {
37+
apiReady.reject();
38+
defer.reject();
39+
console.warn('localStorage not supported in iframe');
40+
}
3541
};
3642
xdLocalStorage.init(options);
3743
return defer.promise;

app/scripts/xdLocalStorage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ window.xdLocalStorage = window.xdLocalStorage || (function () {
88
var options = {
99
iframeId: 'cross-domain-iframe',
1010
iframeUrl: undefined,
11-
initCallback: function () {}
11+
initCallback: function (localStorageSupported) {}
1212
};
1313
var requestId = -1;
1414
var iframe;
@@ -33,7 +33,7 @@ window.xdLocalStorage = window.xdLocalStorage || (function () {
3333
if (data && data.namespace === MESSAGE_NAMESPACE) {
3434
if (data.id === 'iframe-ready') {
3535
iframeReady = true;
36-
options.initCallback();
36+
options.initCallback(data.localStorageSupported);
3737
} else {
3838
applyCallback(data);
3939
}

app/scripts/xdLocalStoragePostMessageApi.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@
1010
var allowedOriginsAttr = (postApi && postApi.getAttribute("accepted-origins")) || '';
1111
var allowedOrigins = allowedOriginsAttr.split(',');
1212

13+
// Checks the browser to see if local storage is supported
14+
var browserSupportsLocalStorage = (function () {
15+
try {
16+
var supported = ('localStorage' in window && window['localStorage'] !== null);
17+
18+
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
19+
// is available, but trying to call .setItem throws an exception.
20+
//
21+
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
22+
// that exceeded the quota."
23+
if (supported) {
24+
var key = '__' + Math.round(Math.random() * 1e7);
25+
localStorage.setItem(key, '');
26+
localStorage.removeItem(key);
27+
}
28+
29+
return supported;
30+
} catch (e) {
31+
return false;
32+
}
33+
}());
34+
1335
// Verify the sender's origin has been whitelisted
1436
function isOriginAllowed(origin) {
1537
if (allowedOrigins.length === 1 && (allowedOrigins[0] === '*' || allowedOrigins[0] === '')) {
@@ -103,7 +125,8 @@
103125
function sendOnLoad() {
104126
var data = {
105127
namespace: MESSAGE_NAMESPACE,
106-
id: 'iframe-ready'
128+
id: 'iframe-ready',
129+
localStorageSupported: browserSupportsLocalStorage
107130
};
108131
parent.postMessage(JSON.stringify(data), '*');
109132
}

0 commit comments

Comments
 (0)