日期:2014-05-16  浏览次数:20404 次

javascript cookies 存、取、删除实例
<script> 

//写cookies函数 作者:翟振凯 
function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值 
{ 
    var Days = 30; //此 cookie 将被保存 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(); 
} 
function getCookie(name)//取cookies函数        
{ 
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); 
     if(arr != null) return unescape(arr[2]); return null; 

} 
function delCookie(name)//删除cookie 
{ 
    var exp = new Date(); 
    exp.setTime(exp.getTime() - 1); 
    var cval=getCookie(name); 
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
} 



SetCookie ("xiaoqi", "3") 
alert(getCookie('xiaoqi')); 
</script> 


一个非常实用的javascript读写Cookie函数
function  GetCookieVal(offset) 
//获得Cookie解码后的值 
{ 
var  endstr  =  documents.cookie.indexOf  (";",  offset); 
if  (endstr  ==  -1) 
endstr  =  documents.cookie.length; 
return  unescape(documents.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  )); 
documents.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); 
documents.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString(); 
} 
function  GetCookie(name) 
//获得Cookie的原始值 
{ 
var  arg  =  name  +  "="; 
var  alen  =  arg.length; 
var  clen  =  documents.cookie.length; 
var  i  =  0; 
while  (i  <  clen) 
{ 
var  j  =  i  +  alen; 
if  (documents.cookie.substring(i,  j)  ==  arg) 
return  GetCookieVal  (j); 
i  =  documents.cookie.indexOf("  ",  i)  +  1; 
if  (i  ==  0)  break; 
} 
return  null; 
} 




<SCRIPT  language="javascript"> 
<!-- 
function  openpopup(){ 
url="popup.htm" 
window.open("gonggao.htm","gonggao","width=260,height=212,left=200,top=0") 
} 

function  get_cookie(Name)  { 
var  search  =  Name  +  "=" 
var  returnvalue  =  ""; 
if  (documents.cookie.length  >  0)  { 
offset  =  documents.cookie.indexOf(search) 
if  (offset  !=  -1)  { 
offset  +=  search.length 
end  =  documents.cookie.indexOf(";",  offset); 
if  (end  ==  -1) 
end  =  documents.cookie.length; 
returnvalue=unescape(documents.cookie.substring(offset,  end)) 
} 
} 
return  returnvalue; 
} 

function  helpor_net(){ 
if  (get_cookie('popped')==''){ 
openpopup() 
documents.cookie="popped=yes" 
} 
} 
helpor_net() 
//--> 
</SCRIPT> 


如果点了确定,只要不清cookie,以后访问都不会再提示,如果不点确定则每次都会提示。放在js文件里,全站包含
<SCRIPT LANGUAGE="JavaScript"> 
<!-- 
var the_cookie = document.cookie; 
var broken_cookie = the_cookie.split(":"); 
var the_visiteraccepted = unescape(broken_cookie[1]); 
// 
if (the_visiteraccepted=="undefined"){ 
        var tmp=confirm('中国人何时何地。'); 
        if(tmp==false){ 
                window.close(); 
        }else{ 
                var the_visiteraccepted = 1;          
        var the_cookie = "ILoveChina=visiteraccepted:" + escape(the_visiteraccepted);                                 
        document.cookie = the_cookie; 
        } 
} 
//--> 
</