IE 11无法获取未定义或空引用的属性“长度" [英] IE 11 Unable to get property 'length' of undefined or null reference
问题描述
我在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屋!