一个非常实用的javascript读写Cookie函数


javascript读写删除cookie的操作.

function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
---------------------------
function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}
---------------------------------
function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
------------------------------------
function GetCookie(name)
//获得Cookie的原始值
{
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;
}
return null;
}



相关阅读:
php与paypal整合方法
轻松玩转Java配置的Classpath
我的论坛源代码(三)
Php利用java解析xml
使用mod_usertrack跟踪用户
为Vista/Win2008中的IIS7添加PHP支持
html教程:解决select菜单边框无法设置的问题
CSS 兼容问题
CSS3模块的目前的状况
CSS教程:简化CSS选择器
Linux学习笔记 Linux目录解释
详解用phpmyadmin建立MYSQL数据库的过程
CSS 字体属性font相关的用法
如何在Linux/FreeBSD下使用U盘
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4