หน้าเว็บ

วันอังคารที่ 23 เมษายน พ.ศ. 2556

cookie serializer javascript

example to use
var values =  [
    { name : 'AAA', value : 'aaa' },
    { name : 'BBB', value : 'bbb' },
    { name : 'CCC', value : 'ccc' }
];

var options = {
    separateKeyValue : ':',
    separateBlock = ',',
    path = '/',
    expire = 1 //day
};

var serializer = new na5cent.CookieSerializer('CKL', values, options); 
//CKL=AAA:aaa,BBB:bbb,CCC:ccc

serializer.get('AAA'); //aaa
serializer.set('AAA', 'xyz');
serializer.get('AAA'); //xyz
serializer.get('BBB'); //bbb
require : jquery.js
require : jquery-cookie.js
/**
 * @module  na5cent.CookieSerializer
 * @author  redcrow
 * @create  24/04/2013
 * @url     http://na5cent.blogspot.com/2013/04/cookie-serializer-javascript.html
 */
var na5cent = {};

na5cent.CookieSerializer = (function($) {

    return function(cookieName, cookieValues, options) {
        var name_ = cookieName;
        var serialize = '';

        var separateKeyValue = options.separateKeyValue || ':';
        var separateBlock = options.separateBlock || ',';
        var path = options.path || '/';
        var expire = options.expire || 365;

        for (var index in  cookieValues) {
            var cookie =  cookieValues[index];
            var name = cookie.name;
            var value = cookie.value;
            serialize = serialize + name + separateKeyValue + value + separateBlock;
        }

        this.set = function(key, value) {
            var windowLayoutCookie = $.cookie(name_);
            if (!windowLayoutCookie) {
                windowLayoutCookie = serialize;
            }

            var fullBlock = '';
            var cookies = windowLayoutCookie.split(separateBlock);
            for (var index in cookies) {
                if (cookies[index] !== '') {
                    var ck = cookies[index].split(separateKeyValue);
                    var cookieKey = ck.shift();
                    var cookieValue = ck.pop();

                    if (cookieKey === key) {
                        cookieValue = value;
                    }

                    fullBlock = fullBlock + cookieKey + separateKeyValue + cookieValue + separateBlock;
                }
            }

            $.cookie(name_, fullBlock, {
                path: path,
                expires: expire
            });
        };

        this.get = function(key) {
            var windowLayoutCookie = $.cookie(name_);
            if (!windowLayoutCookie) {
                windowLayoutCookie = serialize;
                $.cookie(name_, windowLayoutCookie, {
                    path: path,
                    expires: expire
                });
            }

            var value;
            var cookies = windowLayoutCookie.split(separateBlock);
            for (var index in cookies) {
                if (cookies[index] !== '') {
                    var ck = cookies[index].split(separateKeyValue);
                    var cookieKey = ck.shift();
                    var cookieValue = ck.pop();

                    if (cookieKey === key) {
                        value = (cookieValue === '') ? undefined : cookieValue;
                        break;
                    }
                }
            }

            return value;
        };
    };
})(jQuery);

ไม่มีความคิดเห็น:

แสดงความคิดเห็น