Search code examples
javascriptinternet-explorer-10session-storageweb-storage

Trying to use session storage, getting "Unable to set property 'logon' of undefined or null reference


So, I'm trying to make a simple code for a basic javascript class that will maintain a logon state for a user. The function for them to log on is as follows

function userLogon() {
  sessionStorage.logon = "false";
  var name = document.getElementById('userName').value;
  var password = document.getElementById('password').value;

  if (name == "user" && password == "pass") {
    **sessionStorage.logon = "true";**
    alert('Logon successful!');
    window.location = "home.html";
  }

  else {
    alert('Invalid Logon attempt!');
    sessionStorage.logon = "false";
  }
}

I'm just hard-coding in the username and logon since I don't have any database experience yet, and just for testing purposes. I was talking to my teacher about it, e-mailed him my whole website, and he says it's working just fine for him, so how come it doesn't work on my machine, and it gives me this Unable to set property error on the bolded line...

I'm running IE 10.0.11 on Windows 8, and ran a script that checks for session storage compatability, and it came back true... So... It should theoretically work? What the heck is the problem?


Solution

  • I did a quick search on "sessionStorage IE10" and it showed a stackoverflow question just like this. The problem is that sessionStorage is only available in IE 10 if the page is accessed via HTTP (in other words, through a web server). It does not work when opening an HTML page directly from your file system, like "file://C:/Users/blah/test.html"

    session storage not working in IE