前端localStorage用法实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27449993/article/details/79798199

var currentUserIdCon='当前用户id'

var localStroIsDeliveryToDoor="";

获取localStorage的值:


                if (localStorage) {
                    for (var i = 0; i < localStorage.length; i++) {
                        if (localStorage.key(i) == currentUserIdCon) {
                            localStroIsDeliveryToDoor = JSON.parse(window.localStorage ? localStorage.getItem(currentUserIdCon) : Cookie.read(currentUserIdCon));//获取缓存中的5条搜索信息
                        }
                    }

                }
                if (localStroIsDeliveryToDoor != "") {
                    //用于判断缓存的搜索信息是否为空。
                        defaultIsDeliveryToDoor = localStroIsDeliveryToDoor.defaultIsDeliveryToDoor;
                }
                else {
                    //如果缓存的搜索信息为空,将当前的搜索添加到字符串中。
                    defaultIsDeliveryToDoor = false;

                }

设置localStroage的值:

 if (window.localStorage) {
                                localStorage.setItem(currentUserIdCon, JSON.stringify({ 'defaultIsDeliveryToDoor': true, "YJCurrentUserId": currentUserIdCon }));
                            }
                            else {
                                Cookie.write(currentUserIdCon, JSON.stringify({ 'defaultIsDeliveryToDoor': true, "YJCurrentUserId": currentUserIdCon }));
                            }

猜你喜欢

转载自blog.csdn.net/qq_27449993/article/details/79798199
今日推荐