2-Javascript操作Cookie
来源:互联网 发布:淘宝管控记录怎么撤销 编辑:程序博客网 时间:2024/05/06 05:19
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
// 传入3个整型,得到一个从现在开始到cookie过期的时间
function getExpDate(days, hours, minutes) {
var expDate = new Date( );
if (typeof days == "number" && typeof hours == "number" &&
typeof hours == "number") {
expDate.setDate(expDate.getDate( ) + parseInt(days));
expDate.setHours(expDate.getHours( ) + parseInt(hours));
expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
return expDate.toGMTString( );
}
// getCookie( )的工具方法
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) {
endstr = document.cookie.length;
}
return unescape(document.cookie.substring(offset, endstr));
// 通过名称获取Cookie的主方法
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;
}
return "";
// 保存包括其他信息在内的Cookie
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape (value) +
((expires) ? "; expires=" + expires : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
// 通过设置过期来删除指定的Cookie
function deleteCookie(name,path,domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
---------------------------------------------------------------------------------------------
/ 设置Cookie
function setCookie(name,value)
{
var Days = 30;
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
// 获取Cookie
function getCookie(name)
{
// ([^;]*) 表示不是;的0个或多个字符
// (;|$)") 表示;或者结束
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)) return unescape(arr[2]);
else return null;
}
// 删除Cookie
function delCookie(name)
{
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval=getCookie(name);
if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
// 读取cookie:
function readCookie(name)
{
var cookieValue = "";
var search = name + "=";
if(document.cookie.length > 0)
{
offset = document.cookie.indexOf(search);
if (offset != -1)
{
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
cookieValue = unescape(document.cookie.substring(offset, end))
}
}
return cookieValue;
}
// 写入cookie:
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
----------------------------------------------------------------------------------------------------------------------------------------------
<script language="JavaScript">
<!--
var caution = false
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution | | (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex +
prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}
// 得到绝对的时间长度
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}
var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
var visits = getCookie("counter")
if (!visits)
visits = 1
else
visits = parseInt(visits) + 1
setCookie("counter", visits, now)
document.write("欢迎您第 " + visits + " 次来到本站点")
// -->
</script>
- 2-Javascript操作Cookie
- 用JavaScript操作cookie
- javascript 对Cookie操作
- JAVASCRIPT操作Cookie
- 用JavaScript操作cookie
- javascript操作cookie
- JavaScript如何操作cookie
- javascript操作cookie
- javascript操作cookie
- javascript操作cookie
- javascript操作cookie
- javascript 操作cookie类
- javascript 操作cookie类
- Javascript操作cookie
- javascript 操作cookie类
- JavaScript如何操作cookie?
- javascript操作cookie
- javascript操作cookie
- C++程序设计本科教材推荐:适合初学者
- 输入一个整数,判断其是否为素数
- 什么是ALEXA排名
- 经济学原理文章(炒股,炒房必看)
- 重新开始这个博客
- 2-Javascript操作Cookie
- 抽象类
- java Web开发 一(Lomboz 进行Hibernate开发)
- 我的文章
- 快捷键的使用
- by the end of, at the end of , in the end , to the end 的区别与用法
- 实现字符串反转的四种方法(C#)
- 各地人的特点
- 瞎侃....(原创)