1

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> 
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
brodster
  • 77
  • 1
  • 2
  • 12
  • didi you try to remove one equal sign? window.localStorage !== null – EugenSunic Aug 09 '15 at 21:21
  • if i remove one equal sign i get "Sorry, This browser does not support local storage." – brodster Aug 09 '15 at 21:37
  • I believe this could help you: http://stackoverflow.com/questions/21155137/javascript-localstorage-object-broken-in-ie11-on-windows-7/21156133#21156133 – EugenSunic Aug 09 '15 at 21:52
  • that is not the problem, I am running the latest versions of IE 11 – brodster Aug 09 '15 at 22:06
  • @brodster Are you perhaps [accessing the page with `file://`](http://stackoverflow.com/questions/3392032/localstorage-object-is-undefined-in-ie)? Or, have you checked the [document mode](https://msdn.microsoft.com/en-us/library/ff406036.aspx) that's in use? The page may be running under a compatibility mode that doesn't support web storage. – Jonathan Lonowski Aug 10 '15 at 02:31

1 Answers1

1

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'] || '';

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

Anil Talla
  • 709
  • 5
  • 19