I am getting an error in internet explorer 11
"Unable to get property 'length' of undefined or null reference" on line
if (window.localStorage.length !== 0)
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>
I am getting an error in internet explorer 11
"Unable to get property 'length' of undefined or null reference" on line
if (window.localStorage.length !== 0)
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>
Share
Improve this question
edited Aug 10, 2015 at 3:07
Lee Taylor
7,99416 gold badges37 silver badges53 bronze badges
asked Aug 9, 2015 at 21:20
brodsterbrodster
771 gold badge2 silver badges13 bronze badges
5
- didi you try to remove one equal sign? window.localStorage !== null – EugenSunic Commented Aug 9, 2015 at 21:21
- if i remove one equal sign i get "Sorry, This browser does not support local storage." – brodster Commented Aug 9, 2015 at 21:37
- I believe this could help you: stackoverflow./questions/21155137/… – EugenSunic Commented Aug 9, 2015 at 21:52
- that is not the problem, I am running the latest versions of IE 11 – brodster Commented Aug 9, 2015 at 22:06
-
@brodster Are you perhaps accessing the page with
file://
? Or, have you checked the document mode that's in use? The page may be running under a patibility mode that doesn't support web storage. – Jonathan Lonowski Commented Aug 10, 2015 at 2:31
1 Answer
Reset to default 1I 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'] || '';
If localStorage is having some value it will assign to bSupportsLocal, otherwise it is assigned with empty string.