admin管理员组文章数量:1318986
localStorage.setItem
is not working on mobile iOS 8.3.
Does anybody experience this problem?
Here is the code:
var storage = window.localStorage;
storage.setItem('test',45);
alert(storage.getItem('test'));
localStorage.setItem
is not working on mobile iOS 8.3.
Does anybody experience this problem?
Here is the code:
var storage = window.localStorage;
storage.setItem('test',45);
alert(storage.getItem('test'));
Share
Improve this question
edited Aug 6, 2015 at 6:50
Arulkumar
13.2k14 gold badges49 silver badges75 bronze badges
asked May 5, 2015 at 8:38
MandyMandy
211 gold badge1 silver badge2 bronze badges
2 Answers
Reset to default 5In the past, we could use something like:
if ('localStorage' in window && window.localStorage !== null) {
alert('can use');
}else{
alert('cannot use');
}
or
if (localStorage === undefined) {... }
However, in iOS 8.3+, when the user disables cookies, this code throws an unhandled JavaScript error. And when the user goes into private browsing mode, that same error message appears when you try to write to localStorage.
SecurityError: DOM Exception 18: An attempt was made to break through the security policy of the user agent.
Workaround
To avoid unwanted JavaScript errors due to this particular behavior of iOS, one suggestion is to enclose it within try catch blocks. (at least for the time being)
try{
if ('localStorage' in window && window.localStorage !== null) {
localStorage.setItem('testLocalStorage', 'testLocalStorage');
if (localStorage.getItem('testLocalStorage') !== 'testLocalStorage') {
localStorage.removeItem('testLocalStorage');
//for private browsing, error is thrown before even getting here
alert('can read CANNOT write');
}else{
localStorage.removeItem('testLocalStorage');
alert('can use');
}
}else{
alert('CANNOT use');
}
}catch(ex){
alert('CANNOT use reliably');
}
Note: This is not a suggestion to use alerts in your actual codes. It is just for a quick illustration.
Possible Shortform
try {
localStorage.setItem('testLocalStorage', 'testLocalStorage');
localStorage.removeItem('testLocalStorage');
alert('supported');
} catch(ex) {
alert('unsupported');
}
What can we use instead
For scenarios where localStorage is not supported, possible alternatives include server sessions (based on URL parameters if cookies are not supported), or window.name variable to store the serialized data.
Or if you design and build as a Single Page App, perhaps you do not need to store data in the global namespace at all.
You should check if localStorage is available using code like if ('localStorage' in window && window.localStorage !== null) { ...
before you use it.
本文标签: javascriptHTML5 localStoragesetItem is not working on iOS 8Safari mobileStack Overflow
版权声明:本文标题:javascript - HTML5 localStorage.setItem is not working on iOS 8 - Safari mobile - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742056234a2418312.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论