最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - localStorage.getItem returns old data in IE 9 - Stack Overflow

programmeradmin3浏览0评论

The following example needs to be running in IE 9 and in at least two different tabs.

 <input type="text" name="data" value="" placeholder="change me" id="data" />
 <p id="fromEvent">Waiting for data via <code>storage</code> event...</p>

<script type="text/javascript">
window.addEventListener("storage", function (e) {
  if (e.key == 'storage-event-test') {
      var newValue = localStorage.getItem('storage-event-test'); // returns old value
  //  var newValue = e.newValue; // returns new value
      $('#fromEvent').html(newValue);
    }
  }, false);

  $('#data').live('keyup', function () {
     var changedValue = this.value;
      $('#fromEvent').html(changedValue);
       localStorage.setItem('storage-event-test', changedValue);
    });
</script>

If it try to get the data with var newValue = localstorage.getItem('storage-event-test'); and in Tab 1 enters test then it shows correctly test in my <p id="fromEvent"> but in my Tab 2 it only writes tes

Now if I change the code to use var newValue = e.newValue; both Tab 1 & Tab 2 writes test in <p id="fromEvent">

Can someone explain to me, why they return different results? I have also testet this code in Google Chrome and Firefox, and they don't have this problem.

Just for the record, this was running on a win 7 Ultimate 64 SP1 with IIS Express and using jquery-1.5.1. and the bug is in both the 32 and 64 bit version of IE9

Edit Tested with normal IIS 7.5 same result

Edit 2 Would be nice if someone could confirm that this is happening to them to?

The following example needs to be running in IE 9 and in at least two different tabs.

 <input type="text" name="data" value="" placeholder="change me" id="data" />
 <p id="fromEvent">Waiting for data via <code>storage</code> event...</p>

<script type="text/javascript">
window.addEventListener("storage", function (e) {
  if (e.key == 'storage-event-test') {
      var newValue = localStorage.getItem('storage-event-test'); // returns old value
  //  var newValue = e.newValue; // returns new value
      $('#fromEvent').html(newValue);
    }
  }, false);

  $('#data').live('keyup', function () {
     var changedValue = this.value;
      $('#fromEvent').html(changedValue);
       localStorage.setItem('storage-event-test', changedValue);
    });
</script>

If it try to get the data with var newValue = localstorage.getItem('storage-event-test'); and in Tab 1 enters test then it shows correctly test in my <p id="fromEvent"> but in my Tab 2 it only writes tes

Now if I change the code to use var newValue = e.newValue; both Tab 1 & Tab 2 writes test in <p id="fromEvent">

Can someone explain to me, why they return different results? I have also testet this code in Google Chrome and Firefox, and they don't have this problem.

Just for the record, this was running on a win 7 Ultimate 64 SP1 with IIS Express and using jquery-1.5.1. and the bug is in both the 32 and 64 bit version of IE9

Edit Tested with normal IIS 7.5 same result

Edit 2 Would be nice if someone could confirm that this is happening to them to?

Share Improve this question edited Feb 19, 2012 at 8:30 Teemu Ikonen 11.9k4 gold badges24 silver badges35 bronze badges asked Feb 15, 2012 at 11:34 gulbaekgulbaek 2,55113 gold badges44 silver badges65 bronze badges 1
  • 2 This is still an issue in Internet Explorer 11. – Sonny Commented Nov 26, 2014 at 19:54
Add a ment  | 

1 Answer 1

Reset to default 7 +50

As to why they return different results the answer is pretty obvious. The storage event on IE is fired before the value changes, and after on other browsers. You can confirm this by adding a slight delay to your code:

if (e.key == 'storage-event-test') {
    // e.newValue ->  new value
    // localStorage.getItem('storage-event-test') ->  old value in IE
    setTimeout(function(){
        var newValue = localStorage.getItem('storage-event-test'); // new value
         $('#fromEvent').html(newValue);
    }, 1); // delay
}

I don't know why it is implemented this way though, but I'm guessing the specification is too vague and doesn't say when the event should be fired.

The storage event is fired when a storage area changes, as described in the previous two sections (for session storage, for local storage).

When this happens, the user agent must queue a task to fire an event with the name storage, which does not bubble and is not cancelable, and which uses the StorageEvent interface, at each Window object whose Document object has a Storage object that is affected.

发布评论

评论列表(0)

  1. 暂无评论