JavaScript 解析 Cookie 的函数

作者:冷月宫主 来源:冷月学堂 时间:2007-11-08 11:58:00 

JavaScript 读取、删除 Cookie 的函数


*
* WebFXCookie class
*/
function WebFXCookie() { if (document.cookie.length) { this.cookies = ' ' + document.cookie; }
}
WebFXCookie.prototype.setCookie = function (key, value) {
    document.cookie = key + "=" + escape(value);
}
WebFXCookie.prototype.getCookie = function (key) {
    if (this.cookies) {
        var start = this.cookies.indexOf(' ' + key + '=');
        if (start == -1) { returnnull; }
        var end = this.cookies.indexOf(";", start);
        if (end == -1) { end = this.cookies.length; }
        end -= start;
        var cookie = this.cookies.substr(start,end);
        return unescape(cookie.substr(cookie.indexOf('=') + 1, cookie.length - cookie.indexOf('=') + 1));
    }
    else { returnnull; }
}
function getCookieVal (offset) 
{
    var endstr=document.cookie.indexOf (";",offset);if (endstr==-1) 
    endstr=document.cookie.length;return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name)
{
    var arg=name+"=";var alen=arg.length;var clen=document.cookie.length;var i = 0;while (i<clen)
    {
        var 
        j=i+alen;if (document.cookie.substring(i,j)==arg) return getCookieVal (j);i = document.cookie.indexOf(" ",i)+1;if (i==0) 
        break;
    } 
    returnnull;

function DeleteCookie (name) 
{
    var exp=new Date(); exp.setTime (exp.getTime()-1); var cval=GetCookie (name); 
    document.cookie=name+"="+cval+"; expires="+exp.toGMTString();
}


标签:Cookie,JavaScript
0
投稿

猜你喜欢

  • SQL Server连接中经常出现的3个常见错误与解答

    2010-07-26 14:25:00
  • 网页布局的位置重心与位置间的对比关系

    2007-10-15 19:20:00
  • 实际应用:MySQL5存储过程编写

    2008-11-11 12:25:00
  • MySQL 5.0默认100连接数的修改

    2008-11-05 13:34:00
  • 利用XMLHTTP检测网址及探测服务器类型

    2009-04-24 15:12:00
  • 谈谈网页设计中的字体应用 (3) 实战应用篇·上

    2009-11-24 13:09:00
  • asp利用aspjpeg给图片生成PNG透明水印

    2009-03-20 14:01:00
  • 5个提高你站点可读性的方法

    2011-01-31 17:48:00
  • 详细讲解MySQL数据库对文件操作的封装

    2008-12-17 16:08:00
  • 网马解密大讲堂——网马解密中级篇(Document.write篇)

    2009-09-16 16:16:00
  • MySQL 数据库语句优化的原则

    2010-01-20 10:11:00
  • asp如何通过表单创建一个Word?

    2010-06-07 20:56:00
  • 全国哀悼日 建议站点换素装(附代码)

    2008-05-19 12:05:00
  • asp如何写入超长的字符串?

    2010-06-09 18:53:00
  • 模拟兼容性的 inline-block 属性

    2008-04-08 12:37:00
  • removeChild的障眼法

    2009-12-04 12:49:00
  • 启动iis出现发生意外0x8ffe2740的解决方法

    2011-03-31 11:19:00
  • 将表数据生成SQL脚本的存储过程和工具

    2009-01-08 15:24:00
  • CSS背景属性5个应用实例

    2009-09-13 20:54:00
  • 再论Javascript的类继承

    2010-06-26 12:48:00
  • asp之家 网络编程 m.aspxhome.com