论坛首页 Web前端技术论坛

JavaScript清除Cookie完全攻略--被domain和path折磨

浏览 7174 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (9) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-08-20   最后修改:2010-08-20
使用JavaScript清理Cookie首先要找到该Cookie对应的Name对应的值,然后设置其为过期:
找到Cookie有下面方法:
	function getCookie2(name){
		var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));  
    if(arr != null) return unescape(arr[2]); return null;
	}



找到后设置为过期,切记设置domain和path,只有这两个参数跟你要删除的参数完全一样才能把它删除掉。

function resetNfluent(){
		alert("before=>"+document.cookie);
		var exp  = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval=getCookie2('name');
		var lanObj=document.getElementById('lanOption');
		var lanSel=lanObj.value;
		alert(lanSel);
		if(lanSel=='en'){
			alert('let\'s reset nFluent');
			alert('cval=>'+cval);
			if(cval!=null){
				document.cookie="name="+cval+";domain=.example.com;expires="+exp.toGMTString()+";path=/";
			}else{
				document.cookie="name=;domain=.example.com;expires="+exp.toGMTString()+";path=/";
				}
		}else{
			alert('Don\'t need reset nFluent');
			}
		alert("after=>"+document.cookie);
	}



至于如何详细查看cookie的path和domain之类的东西,在firefox中可以根据以下步骤查看:
Tools->Option->Privacy->Show Cookie


  • 大小: 45.9 KB
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics