admin管理员组文章数量:1297077
I'm looking for suggestions for how to keep my web form from breaking when users have sessionStorage disabled due to browser settings. For example, I get the following error in Chrome when browser settings block 3rd party cookies:
Uncaught DOMException: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.
I have two problems:
The exception breaks the functionality of my form on Chrome when 3rd party cookies are blocked. I would like the user to still be able to use the form and just use a fallback.
My test condition isn't working because it isn't picking up a falsey value so I can't display a message. I would like to give the user instructions on how to change the settings so they can get full functionality if they want. Is there a way to write a test condition for a DOMexception?
Here is the code I'm testing with:
// Test if sessionStorage available
if(!sessionStorage) {
alert("Sorry, your browser does not support session storage.");
} else {
// Store data
sessionStorage.setItem("name", "John Smith");
// Retrieve data
alert("Hi, " + sessionStorage.getItem("name"));
}
I'm looking for suggestions for how to keep my web form from breaking when users have sessionStorage disabled due to browser settings. For example, I get the following error in Chrome when browser settings block 3rd party cookies:
Uncaught DOMException: Failed to read the 'sessionStorage' property from 'Window': Access is denied for this document.
I have two problems:
The exception breaks the functionality of my form on Chrome when 3rd party cookies are blocked. I would like the user to still be able to use the form and just use a fallback.
My test condition isn't working because it isn't picking up a falsey value so I can't display a message. I would like to give the user instructions on how to change the settings so they can get full functionality if they want. Is there a way to write a test condition for a DOMexception?
Here is the code I'm testing with:
// Test if sessionStorage available
if(!sessionStorage) {
alert("Sorry, your browser does not support session storage.");
} else {
// Store data
sessionStorage.setItem("name", "John Smith");
// Retrieve data
alert("Hi, " + sessionStorage.getItem("name"));
}
Share
asked Sep 29, 2019 at 4:27
dumdum3000dumdum3000
3353 gold badges7 silver badges14 bronze badges
2 Answers
Reset to default 5Edit - My previous answer won't handle your scenario, hence exception handling will be required.
try {
var storage = window.sessionStorage || {};
} catch (e) {
var storage = {};
}
use try catch
.
try {
sessionStorage.setItem("name", "John Smith");
alert('Hi, ' + sessionStorage.getItem("name"));
} catch(e) {
console.log('Session Storage is disabled');
}
本文标签: javascriptWhat to do when sessionStorage access denied due to browser settingsStack Overflow
版权声明:本文标题:javascript - What to do when sessionStorage access denied due to browser settings - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741615036a2388478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论