我有localStorage
的以下代码:
function supports_html5_storage()
{
try {
return 'localStorage' in window && window['localStorage'] !== null;
}
catch (e) {
return false;
}
}
function setFormFieldValues()
{
if (supports_html5_storage()) {
var retrievedUserDataObj = JSON.parse(localStorage.getItem('UserDataObj'));
if (retrievedUserDataObj) {
...
}
}
}
现在这在Firefox和Chrome中运行良好,但在IE8中,我收到以下错误:
无法获取属性'getItem'的值:object为null或undefined
答案 0 :(得分:2)
试试这个。如果您已经在使用try/catch
,那就更直接了。
function supports_html5_storage() {
try {
window.localStorage.setItem( 'checkLocalStorage', true );
window.localStorage.removeItem( 'checkLocalStorage' );
return true;
} catch ( error ) {
return false;
};
};
document.getElementById( 'result' ).textContent =
'localstorage: ' + supports_html5_storage();
<div id="result"></div>