LocalStorage 浏览器缓存

使用说明

本框架使用axLocalStorage来存储大型数据,小型数据缓存请使用axCookie存储,axLocalStorage格式如下:

注意:axLocalStorage不能存储DOM节点和函数对象。

                            <script type="text/javascript">
                            axLocalStorage.set('key', 'value', {
                                type:'d',
                                expires: 7,
                            });
                            </script>
                        

一般简写如下:

                            <script type="text/javascript">
                            axLocalStorage.set('key', 'value');
                            </script>
                        
  • key是设置localstorage名称
  • value是设置localstorage的值
  • type是设置过期时间单位,可选择second或s,minite或m,hour或h,day或d,默认day。
  • expires是设置localstorage的有效期。可以设置一个整数作为有效期(单位随type),默认为0即永久存储;也可以设置一个日期对象为LocalStorage的过期日期,比如"2024-1-23";如果为负数,那么此LocalStorage将被删除。
  • cover是设置覆盖同名属性,true完全覆盖,false则扩展属性,默认true。

设置localstorage

                            <script type="text/javascript">
                            axLocalStorage.set('key', 'value');
                            </script>
                        

读取localstorage

                            <script type="text/javascript">
                            axLocalStorage.get('key');
                            </script>
                        

删除localstorage

                            <script type="text/javascript">
                            axLocalStorage.set('key', null);
                            //axLocalStorage.set("key", '', { expires: -1 });设置过去时间也等于删除
                            //axLocalStorage.remove('key');使用remove方法删除一个
                            //axLocalStorage.remove(['key01','key02']);使用remove方法批量删除
                            </script>
                        

实例1:点击按钮设置localstorage值

                            <script type="text/javascript">
                            var elem = document.querySelector("#id");
                            var content = document.querySelector("#show");
                            elem.onclick = function(){
                                axLocalStorage.set('key', 'value');
                                content.textContent = axLocalStorage.get('key');
                            };
                            </script>
                        

实例2:点击按钮查看localstorage值

                            <script type="text/javascript">
                            var elem = document.querySelector("#id");
                            var content = document.querySelector("#show");
                            elem.onclick = function(){
                                content.textContent = axLocalStorage.get('key');
                            };
                            </script>
                        

实例3:点击按钮清除localstorage值

                            <script type="text/javascript">
                            var elem = document.querySelector("#id");
                            elem.onclick = function(){
                                axLocalStorage.remove('key')
                            };
                            </script>
                        

实例4:过期时间定义到分

                            <script type="text/javascript">
                            axLocalStorage.set('key', 'value', {
                            type:'m',
                            expires : 60
                            });
                            }
                            </script>