Heim > Web-Frontend > js-Tutorial > Hauptteil

js操作cookie实例

小云云
Freigeben: 2018-03-27 17:28:15
Original
1145 人浏览过

本文主要和大家分享js操作cookie实例,主要以代码的形式和大家分享,希望能帮助到大家。

/**
 * 
 * @param {有效期} maxage 
 * @param {作用域} path 
 */
function cookieStorage(maxage, path) {
    var cookie = (function () {
        var cookies = {}; //该对象会最终返回
        var all = document.cookie; //一大些字符串的形式获取所有cookie信息
        if (all === "") {
            return cookie; //返回一个空对象
        }
        var list = all.split(";"); //分离出键值对
        for (var i = 0, len = list.length; i < len; i++) {
            var cookie = list[i];
            var p = cookie.indexOf("="); //查找第一个"="符号
            var name = cookie.substring(0, p); //获取cookie名字
            var value = cookie.substring(p + 1); //获取cookie对应的值
            value = decodeURIComponent(value); //对其值进行解码
            cookies[name] = value;
        }
        return cookies;
    }());

    //将所有cookie的名字存储到一个数组中
    var keys = [];
    for (var key in cookie) {
        keys.push(key);
    }

    //现在定义存储api公共的属性和方法
    //存储的cookie的个数
    this.length = keys.length;

    //返回第n个cookie的名字,如果n越界则返回null
    this.key = function (n) {
        if (n < 0 || n >= keys.length) {
            return null;
        }
        return keys[n];
    }

    //返回指定名字的cookie值,如果不存在则返回null
    this.getItem = function (name) {
        return cookie[name] || null;
    }

    //存储cookie值
    this.setItem = function (key, value) {
        if (!(key in cookie)) { //如果要存储的cookie还不存在
            keys.push(key);
            this.length++; //cookie个数加一
        }

        //将该键值对存储到cookie对象中
        cookie[key] = value;

        //开始正式设置cookie
        //首先将要存储的cookie的值进行编码,同时创建一个“名字=编码后的值”形式的字符串
        var cookie = key + "=" + encodeURIComponent(value);

        //将cookie的属性也加入到该字符串中
        if (maxage) cookie += ";max-age=" + maxage;
        if (path) cookie += ";path=" + path;

        //通过document.cookie属性来设置cookie
        document.cookie = cookie;
    };

    //删除指定的cookie
    this.removeItem = function(key){
        if(i(key in cookie)){
            return;
        }

        //从内部维护的cookie组删除指定的cookie
        delete cookie[key];

        //同时将cookie中的名字也在内部的数组中删除
        //如果使用es5定义的数组indexof方法会更简单
        for(var i = 0, len = keys.length ; i
Nach dem Login kopieren

相关推荐:

cookie的原理和现象分析

PHP会话控制之cookie详解

cookie的用法详细讲解

以上是js操作cookie实例的详细内容。更多信息请关注PHP中文网其他相关文章!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!