H5本地存储

在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");
        }

  

posted @ 2015-09-12 11:41  leechg  阅读(565)  评论(0编辑  收藏  举报