1

I'm working on a local html file, stored on a Win7 machine and opened in IE 9. This html file uses javascript and jStorage.

However, when I run, I get the error "SCRIPT5007: Unable to get value of the property 'set': object is null or undefined." This error points to the statement $.jStorage.set("Key", "Hello");.

What am I doing wrong? I've made the html and javascript about as basic as I can, to narrow things down.

Here's the html:

<!DOCTYPE html>
<html>
    <head>
    <title>Backlog Tracker</title>
    <script src="jquery-2.1.1.min.js"></script>
    <script src="json2.js"></script>
    <script src="jstorage.min.js"></script>
    <script src="backlog.js"></script>
    </head>
    <body>
    </body>
</html>

... and, here's the script (referenced as "backlog.js" in the html):

$(document).ready(function(){
    $("body").append("<button>Try It</button>");
    $("button").click(function(){
    $.jStorage.set("Key", "Hello");
    console.log($.jStorage.get("Key"));
    });
});

As a side note, I've read other questions on SO, such as here, but nothing seems to explain this. Reference jStorage usage here, everything seems to be in order. I was originally pointed to jStorage thanks to this SO answer.

Community
  • 1
  • 1
Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • 1
    Are you accessing this HTML file simply as a file, or is it part of a local server system (ie, localhost:80/page.html)? localStorage values must be associated with a hostname, and I doubt the browser can treat "file://" as a hostname. – Katana314 Jun 16 '14 at 14:19
  • It's accessed simply as a file. localStorage will not work, but based on one of the links above(http://stackoverflow.com/a/9163135/2658159) I was hoping jStorage would be a work-around. – Aaron Thomas Jun 16 '14 at 14:21
  • The error message suggests `$.jStorage` is not defined - if you do `console.log($.jStorage)` what do you get? I assume there are no loading errors earlier in the log? – CupawnTae Jun 16 '14 at 14:23
  • @CupawnTae: no loading errors earlier in the log. If I insert `console.log($.jStorage)` before `$.jStorage.set("Key", "Hello");`, I get "LOG: undefined" – Aaron Thomas Jun 16 '14 at 14:45
  • Ok, so the problem is definitely that jStorage isn't loaded correctly. It's possible that @Mooseman's answer explains the reason for this so I'd suggest you make the updates he proposes and see if it helps. – CupawnTae Jun 16 '14 at 14:47
  • Could you try this please?: http://jsfiddle.net/5hjj8/1/ It's working here. – Niek Vandael Jun 17 '14 at 14:02

1 Answers1

1

localStorage does not work for local files in IE. It does in Chrome but not in IE.

Also IE 9 does support JSON if you have <!DOCTYPE html> and <meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1" /> in your document.

Try to run a small local web server.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • Does this explain the behaviour? jStorage is supposed to provide fallbacks, and "If the browser doesn't support data caching, then no exceptions are raised - jStorage can still be used by the script but nothing is actually stored." (http://www.jstorage.info/). – CupawnTae Jun 20 '14 at 08:09
  • Could you please check if `$.jStorage.storageAvailable()` and what the `$.jStorage.currentBackend()` is? – PiTheNumber Jun 20 '14 at 08:16
  • Have you tried using `jquery.json-2.3.min.js` instead of `json2.js`, see http://www.jjask.com/300384/jquery-jstorage-issue-on-ie8? – PiTheNumber Jun 20 '14 at 08:23
  • `$.jStorage` is undefined - see the comments on the question. So `$.jStorage.storageAvailable()` will just throw an error – CupawnTae Jun 20 '14 at 08:55
  • Does it work with the [suggested](http://www.jstorage.info/#usage) include code? It could be a version problem. Also you might want to switch to the newer version [simpleStorage](https://github.com/andris9/simpleStorage) if you do not need IE7. – PiTheNumber Jun 20 '14 at 11:05
  • @PiTheNumber sorry, adding doesn't change this. Still getting same error in the same place. – Aaron Thomas Jun 20 '14 at 14:51
  • @PiTheNumber: attempted to check $.jStorage.storageAvailable(), but got error "Unable to get value of the property 'storageAvailable': object is null or undefined" – Aaron Thomas Jun 20 '14 at 14:54
  • @AaronThomas Does it work with the [suggested](http://www.jstorage.info/#usage) include code? It could be a version problem. Also you might want to switch to the newer version [simpleStorage](https://github.com/andris9/simpleStorage) if you do not need IE7. – PiTheNumber Jun 25 '14 at 06:03