function setCookie(name,value,hours,path){
var name = escape(name);
var value = escape(value);
var expires = new Date();
expires.setTime(expires.getTime() + hours*3600000);
path = path == "" ? "" : ";path=" + path;
_expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();
document.cookie = name + "=" + value + _expires + path;
}
以上为设置一个值到cookie中, 删除的时候,
function delCookie(name){
// setCookie(name,"",{expireDays:-1});
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
} 为什么这个方法delCookie()没有用呢,
我在网上找的代码都不能删除很是郁闷,
请各位帮我写个删除cookie的方法, 到底如何删除 cookie
谢谢啦,
var name = escape(name);
var value = escape(value);
var expires = new Date();
expires.setTime(expires.getTime() + hours*3600000);
path = path == "" ? "" : ";path=" + path;
_expires = (typeof hours) == "string" ? "" : ";expires=" + expires.toUTCString();
document.cookie = name + "=" + value + _expires + path;
}
以上为设置一个值到cookie中, 删除的时候,
function delCookie(name){
// setCookie(name,"",{expireDays:-1});
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
} 为什么这个方法delCookie()没有用呢,
我在网上找的代码都不能删除很是郁闷,
请各位帮我写个删除cookie的方法, 到底如何删除 cookie
谢谢啦,
解决方案 »
- 如何提高extjs的下载速度?网上介绍的都是java的方法,现求C#下使用的方法?
- Jquery中,遍历所有文本框后提交表单
- if($("#hui_tab1").find("#"+$val+"").is(":hidden")) {} 是什么意思?
- 這裡的this.parentNode和this.parentNode.nextSibling指什麼?
- 求助一个简单的问题
- IE10 下如何保证离开iframe窗口仍能保持选中区?
- 调用DLL
- 关于wscript.shell run参数的问题
- JAVASCRIPT两个问题,没分了,但还是要感谢。
- 关于javascript文件名的问题
- 求教一个JQuery源码的问题,请指点下
- 关于sort()方法的问题
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<style>
*{ font-size:12px;}
a{ color:#3399FF}
a:hover{ color:##3399FF}
a.hover{ color:#CC3300}
</style>
</head>
<body>
<div id="menu">
<a href="javascript:void(0)" class="hover" onclick="changename(0)">我是第一</a>
<a href="javascript:void(0)" onclick="changename(1)">我是第二</a>
<a href="javascript:void(0)" onclick="changename(2)">我是第三</a>
<a href="javascript:void(0)" onclick="changename(3)">我是第四</a>
</div>
<a href="javascript:clear();">点我清除cookie</a>
<script language="javascript">
function changename(c,cl)
{
var d=document.getElementById("menu").getElementsByTagName("a");
if(!cl)
{
writeCookie("hovers",c,222);
}
c=readCookie("hovers")?readCookie("hovers"):c;
for(i=0;i<d.length;i++)
{
d[i].className=i==c?"hover":"";
}
}
function writeCookie(name, value, hours)
{
var expire = "";
if(hours != null)
{
expire = new Date((new Date()).getTime() + 60 * 60 *1000);//+ hours
expire = "; expires=" + expire.toGMTString();
}
document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
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;
}
function clear()
{
writeCookie("hovers","",222);
document.location=document.location.href;
}
changename(0,1)
</script>
</body>
</html>
function addCookie(objName,objValue,objHours){
var str = objName + "=" + escape(objValue);
if(objHours > 0){//为0时不设定过期时间,浏览器关闭时cookie自动消失
var date = new Date();
var ms = objHours*3600*1000;
date.setTime(date.getTime() + ms);
str += "; expires=" + date.toGMTString();
}
document.cookie = str;
alert("添加cookie成功");
}
//获取指定名称的cookie的值
function getCookie(objName){
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == objName) return unescape(temp[1]);
}
}
//为了删除指定名称的cookie,可以将其过期时间设定为一个过去的时间
function delCookie(name){
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = name + "=a; expires=" + date.toGMTString();
}
(function($) {
$.cookie = {
enabled: navigator.cookieEnabled,
getMap: function() {
var _cookieItems = document.cookie.split(';');
var _map = {};
if(_cookieItems.length && $.trim(_cookieItems[0]) != '') {
$.each(_cookieItems, function(i, v) {
var _item = v.split('=');
_map[decodeURI($.trim(_item[0]))] = decodeURI($.trim(_item[1]));
});
}
return _map;
},
get: function(key) {
var _map = this.getMap();
return _map[key]? _map[key] : null;
},
/**
* op = {
* key: 'name',
* path: 'storage path',
* domain: 'domain',
* }
**/
del: function(op) {
var _v = this.get(key);
if(_v != null) {
this.set($.extend({
value: '',
time: -1
}, op));
}
},
/**
* op = {
* key: 'name',
* value: 'value',
* time: 'expires' units is seconds
* path: 'storage path',
* domain: 'domain',
* len: how many values the name can have
* flag: ',' if the name have some values, then use the flag to join them
* }
**/
set: function(op) {
if(op.key && typeof(op.value) != 'undefined' && op.value != null) {
var _cookie = [];
var _v = this.get(op.key);
var _flag = op.flag? op.flag : '|';
var _len = op.len? op.len : 1;
if(_len > 1 && _v) {
_v = _v.split(_flag);
if(_v.length >= _len) {
_v.splice(_len -1, _v.length - _len + 1);
}
op.value = op.value + _flag + _v.join(_flag);
}
_cookie.push(op.key + '=' + encodeURI(op.value));
if (op.time) {
var _d = new Date();
_d.setTime(_d.getTime() + (op.time * 1000));
_cookie.push('expires=' + _d.toGMTString());
}
if (op.domain) {
_cookie.push('domain=' + op.domain);
}
if (op.path) {
_cookie.push('path=' + op.path);
}
document.cookie = _cookie.join(';');
}
}
};
})(jQuery);