本地存储柜台

时间:2017-09-12 18:18:08

标签: javascript html html5 stack-overflow

这是一个JavaScript localstorage计数器代码:



if (typeof(Storage) !== "undefined") {
  var n = localStorage.getItem('on_load_counter');
  if (n === null) {
    n = 0;
  }
  n++;
  localStorage.setItem("on_load_counter", n);
  document.getElementById('counter').innerHTML = n;
  if (n > 3) {
    alert('3rd time');
    return;
  }
} else {
  document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}

<div id="counter"></div>
<div id="result"></div>
&#13;
&#13;
&#13;

此代码无效。问题出在哪儿? 怎么解决? 如果我们删除Web存储支持检查代码,则其他代码可以正常工作。

1 个答案:

答案 0 :(得分:0)

当我尝试删除返回时;在JavaScript中,代码运行完美。 这是代码:

<script>
  if (typeof(Storage) !== "undefined") {
    var n = localStorage.getItem('on_load_counter');
    if (n === null) {
      n = 0;
    }
    n++;

    localStorage.setItem("on_load_counter", n);
    document.getElementById('counter').innerHTML = n;
    if (n > 3) {
      alert('3rd time');
    }
  } else {
    document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
  }
</script>