【问题标题】:HTML5 localStorage getItem issue in IE8IE8 中的 HTML5 localStorage getItem 问题
【发布时间】:2013-03-10 05:00:28
【问题描述】:

我有以下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”的值:对象为空或未定义

【问题讨论】:

  • 这已在其他一些帖子中得到解答。看看这个stackoverflow.com/questions/3452816/…
  • @Aiias 我在那篇文章中没有看到答案。
  • 我不认为这是一个骗局。在另一个问题中,声明window.localStorage 未定义。在这里,OP 说缺少 window.localStorage 的属性。
  • 啊,我认为这是由于未定义 DOCTYPE 而导致 window.localStorage 返回 null 所致。 IE8 应该支持getItem。见MSDN documentationgetItem
  • 我的文档类型定义为 ...在 IE8 中仍然无法使用

标签: javascript html internet-explorer-8 local-storage


【解决方案1】:

试试这个。如果您已经在使用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();

HTML:

<div id="result"></div>

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 2014-07-11
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多