在HTML5中可以把数据长期存储在客户端,使用的对象就是localStorage。

localStorage常用方法有setItem、getItem、removeItem、clear。

下面是一个存储数组对象的例子,由于localStorage中存储的数据会自动转换为字符串,数组类型则会自动join(","),所以数组元素中最好不要有','。

        function getHistory() {
            var _history = localStorage.getItem("routeHistory");
            if (_history) {
                return _history.split(",");
            }
            return [];
        }
        function addHistory(newhis) {
            var _history = getHistory();
            if (_history.length >= 10) {
                _history.pop();
            }
            _history.reverse();
            _history.push(newhis);
            _history.reverse();
            localStorage.setItem("routeHistory", _history);
        }
        function clearHistory() {
            localStorage.removeItem("routeHistory");
        }

  

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案