写自己的代码之封装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
- 写自己的代码之封装Cookie
- 封装自己的cookie库
- 自己写的关于sqlite的封装代码
- 写自己的代码之js狂想曲
- 写自己的代码之未解决
- 数据结构实验之栈六:下一较大值(二)【OJ-3333】【自己写的封装的栈代码】
- 数据结构实验之栈六:下一较大值(二)【OJ-3333】【自己写的封装的栈代码】
- 自己封装的加密cookie相关函数
- 写自己的代码之Ext.extend仿写
- 写自己的代码之Ext.OnReady仿写
- 自己写的代码
- 自己封装的jdbc代码
- 自己写的封装的DBHelper
- 自己写的一个参数封装组件
- Kad -- 自己写的JS浮动广告代码封装器 支持FF/IE
- C# 一个自己写的树结构代码(2)-Array,HashTable,List,String数据结构操作封装
- 写自己的代码之js粗浅框架笔记
- 写自己的代码之Ext/js学习笔记
- 内省、JavaBean、PropertyDescriptor类、Introspector类、BeanUtils工具包、注解、Rentention、Target、注解的基本属性和高级属性
- uboot 下脚本的开发
- Outlook 2010 can't open Https:// hyperlink
- gridview激发了未处理的事件RowDeleting
- Union和Union All
- 写自己的代码之封装Cookie
- ORACLE操作指南及问题解决
- 从WordCount看hadoop原理(2)——Reduce部分浅析
- MAC下的SVN服务器建立
- Struts2 返回 json 的数组数据为 "空" 的解决方案
- mysql performance schema
- 教大家在CSDN里面上传GIF格式图片
- OAuth 2.0
- 【二分图匹配(最小顶点覆盖)】hdu 1498 50 years,50 colors