Cookie object





10
Date Submitted Wed. Mar. 14th, 2007 12:06 PM
Revision 1 of 1
Scripter shachi
Tags Cookie | JavaScript | object
Comments 0 comments
This is a simple cookie object that handles some of the basic cookie functions and which is kind of like a simple cookie-object parser.

Hope you like it.


var Cookie = {
       
        exists : function(name){
                var ca = document.cookie.split(";");
                var str = "";
                for(var i in ca){
                        var n = ca[i].split("=");
                        var non = n[0];
                        non = non.replace(/^\s/, "");
                        str += non+";";
                }
                if(str.indexOf(name) > -1){
                        return true;
                } else {
                        return false;
                }
        },

        create : function(name, value, days){
                if (days) {
                        var date = new Date();
                        date.setTime(date.getTime()+(days*24*60*60*1000));
                        var expires = "; expires="+date.toGMTString();
                } else {
                        var expires = "";
                }
                        document.cookie = name+"="+escape(value)+expires+"; path=/";
        },
       
        allNames : function(){
                var ca = document.cookie.split(";");
                var o = [];
                for(var i in ca){
                        var c = ca[i];
                        var non = c.substring(0,c.indexOf("="));
                        var ftype = non.substr(non.indexOf(":")+1);
                        non = non.substring(0, non.indexOf(":"));
                        non = non.replace(/^\s/, "");
                        o[non] = ftype;
                }
                return o;
        },
       
        getNVpair : function(){
                var ca = document.cookie.split(";");
                var o = {};
                for(var i in ca){
                        var c = ca[i];
                        var n = c.split("=");
                        for(var i in n){
                                var non = n[0];
                                non = non.replace(/^\s/, "");
                                o[non] = new Object();
                                o[non]["value"] = n[1];
                        }
                }
                return o;
        },
       
        read : function(name){
                var nameEQ = name + "=";
                var ca = document.cookie.split(';');
                for(var i=0;i < ca.length;i++) {
                        var c = ca[i];
                        while (c.charAt(0)==' ') c = c.substring(1,c.length);
                        if (c.indexOf(nameEQ) == 0){
                        return unescape(c.substring(nameEQ.length,c.length));
                        }
                }
                return null;
        },
       
        destroy : function(name){
                        if(name != "*"){
                                this.create(name, "", -1);
                        } else {
                                var cs = this.getNVpair();
                                for(var i in cs){
                                        this.destroy(i);
                                }
                        }
        }
}

 

Cookie.create("cookCookie", "ummm ... tasty cookie", 7); // creates a cookie
Cookie.create("cookie_two", "some value", 7); // creates another cookie
var cN = Cookie.allNames(); // returns an associative array with all the names of the cookie
var cO = Cookie.getNVpair(); // returns an object with cookie names and their respective values, the form in this case would be: Object > Object:cookCookie >> cookCookie["value"] = "ummm ... tasty cookie"; Object:cookie_two >> cookie_two["value"] = "some value"
for(var i in cO){
alert(Cookie.read(i));
}
Cookie.destroy("*"); // .. or ..
/*Cookie.destroy("cookCookie");
Cookie.destroy("cookie_two");*/

 

Shachi Bista

Comments

There are currently no comments for this snippet.

Voting