写自己的代码之封装Cookie

来源:互联网 发布:linux服务开机自启动 编辑:程序博客网 时间:2024/05/14 19:51

尝试封装了一下Cookie,这里没有涉及到path 和expires 因为我还没弄明白其中的意思。

var Cookie = function(){this.version = '1.0';return  new Cookie.prototype.init();                                    //为了获取prototype中的方法必须new Cookie();如果按照这种写法会造成堆栈溢出}Cookie.prototype = {addCookie: function(key,value){Document.cookie = key+"="+escape(value); //"key=value"; 第一步简化只有key 和 value},getCookie: function(){return Document.cookie;},isExistKey: function(key){if(!key || key == 'undefined'){           //如果是null 或者是defined则返回return;}var cookies = Document.cookie;var ary = cookies.split(';');for(var index = 0; index < ary.length; index++){var temp = ary[index].split('=');var tempKey = temp[0];if(key == tempKey){return true;}}return false},getSpecilValue: function(key){if(!key || key == 'undefined'){return;}var cookie = Document.cookie;var ary = cookie.split(';');alert(index);   //测试一下上面的index是否对这里index造成影响 ,输出的是undefine..for(var index = 0; index < ary.length; index++){var temp = ary[index].split('=');var tempKey = temp[0];if(tempKey == key){return temp[1];}}return null;},init: function(){return this;}}Cookie.prototype.init.prototype = Cookie.prototype;           //将Cookie.prototype中扩展出来的方法放到init.prototype中,这样Cookie中return中new的对象才能访问//下面是测试Cookie().addCookie('name','shangguanls');var cookie = Cookie().getCookie();alert(cookie);//测试isExistKeyvar flag = Cookie().isExistKey('name');alert(flag);//var value = Cookie().getSpecilValue('name');alert(value);

这也算是对前面探索js一些零碎知识的一些应用,写的不是很好。


modify in 2014/1/7

对addCookie做了修改增加了 path expires 等参数; 对getSpecilValue获取做了修改,这是另外一种获取的写法

var Cookie = function(){this.version = '1.1';return  new Cookie.prototype.init();                                    //为了获取prototype中的方法必须new Cookie();如果按照这种写法会造成堆栈溢出}Cookie.prototype = {addCookie: function(key,value){var param = argumentsvar len = arguments.length;var date = len > 2 ? param[2] : null;var path = len > 3 ? param[3] : '/';var domain = len > 4 ? param[4] : null;var secure = len > 5 ? param[5] : false;Document.cookie = key + "=" + escape(value) + ((date === null) ? "": ("; expires=" + date.toGMTString())) +  //"key=value"; 第一步简化只有key 和 value((path === null) ? '' : ("; path=" +path)) + ((domain === null) ? "": (";domain=") + domain) +((secure === true) ? "; secure": "");},getCookie: function(){return Document.cookie;},isExistKey: function(key){if(!key || key == 'undefined'){           //如果是null 或者是defined则返回return;}var cookies = Document.cookie;var ary = cookies.split(';');for(var index = 0; index < ary.length; index++){var temp = ary[index].split('=');var tempKey = temp[0];if(key == tempKey){return true;}}return false},getSpecilValue: function(key){var temp = key+'=';var size = temp.length;var cookie = document.cookie;var length = document.cookie.length;var pos = 0;while(pos < length){var location = pos + size;  //获取key=的位置if(cookie.substring(pos,size) == temp){return Cookie.prototype.getValue(location);}//cookie字符串中如果包含多个key和value的形式,每个之间用 ' '隔开//第一个key value 找不到,找下一个pos = cookie.indexOf(' ',location)+1; //这里用location和pos都可以,表示从指定位置开始找' 'if(pos == 0){   //如果找不到' ' 说明结束退出    pos  -1 + 1 = 0break;}}return null;},init: function(){return this;},getValue: function(location){var pos = document.cookie.indexOf(';',location);if(pos == -1){pos = document.cookie.length;}return unescape(document.cookie.substring(location,pos));  //addCookie使用用escape编码,所以这里需要用unescape进行解码}}Cookie.prototype.init.prototype = Cookie.prototype;           //将Cookie.prototype中扩展出来的方法放到init.prototype中,这样Cookie中return中new的对象才能访问//下面是测试Cookie().addCookie('name','shangguanls');var cookie = Cookie().getCookie();alert(cookie);//测试isExistKeyvar flag = Cookie().isExistKey('name');alert(flag);//var value = Cookie().getSpecilValue('name');alert(value);




0 0