IE 11无法获取未定义或空引用的属性“长度" [英] IE 11 Unable to get property 'length' of undefined or null reference

查看:683
本文介绍了IE 11无法获取未定义或空引用的属性“长度"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Internet Explorer 11中遇到错误

I am getting an error in internet explorer 11

无法获取未定义或空引用的属性'length'" 线

"Unable to get property 'length' of undefined or null reference" on line

if (window.localStorage.length !== 0)

它在chrome和Firefox上运行良好,不确定是什么原因造成的

it works fine on chrome and Firefox, not sure whats causing it

<script>
    function initialize() {
        // test to see if brouser supports storeage api
        var bSupportsLocal = (('localStorage' in window) && window.localStorage !== null );

        if (!bSupportsLocal) {
            document.getElementById('infoform').innerHTML = "<p> Sorry, This browser does not suport local storage. </p>";
            return;
        }

        if (window.localStorage.length !== 0) {
        document.getElementById('firstName').value = window.localStorage.getItem('firstName');
        $.mobile.navigate("#benefits-facts");
        }
    }

    function storeLocalContent(fName) {
        window.localStorage.setItem('firstName', fName);

        }

        function clearLocalContent(strToStore) {
            window.localStorage.clear();

            }

    window.onload = initialize;
    </script> 

推荐答案

我认为IE window.localStorage最初是未定义的.您正在检查window中的localStorage,而不是null.因此bSupportLocal设置为true.它的执行window.localStorage.length语句. Undefined.length导致错误. 这是代码

I thought in IE window.localStorage is undefined initially. You are checking is localStorage in the window and its not null. So bSupportLocal is setting to true. Its executing window.localStorage.length statement. Undefined.length causing error. Here is the code

     var bSupportsLocal = window['localStorage'] || '';

如果localStorage具有某个值,它将分配给bSupportsLocal,否则将分配一个空字符串.

If localStorage is having some value it will assign to bSupportsLocal, otherwise it is assigned with empty string.

这篇关于IE 11无法获取未定义或空引用的属性“长度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆