javascript控制cookies及在跳出本页给出提示,是否放弃操作!!

1.html
None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
None.gif
< HTML >
None.gif
< HEAD >
None.gif
< TITLE >  New Document  </ TITLE >
None.gif
< META  NAME ="Generator"  CONTENT ="EditPlus" >
None.gif
< META  NAME ="Author"  CONTENT ="" >
None.gif
< META  NAME ="Keywords"  CONTENT ="" >
None.gif
< META  NAME ="Description"  CONTENT ="" >
None.gif
</ HEAD >
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="JavaScript" > dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Setcookie(name, value) dot.gif//设置名称为name,值为value的Cookie 
InBlock.gif
    //var argc = SetCookie.arguments.length; 
InBlock.gif
    //var argv = SetCookie.arguments; 
InBlock.gif
    //var path = (argc > 3) ? argv[3] : null; 
InBlock.gif
    //var domain = (argc > 4) ? argv[4] : null; 
InBlock.gif
    //var secure = (argc > 5) ? argv[5] : false; 
InBlock.gif

InBlock.gif
InBlock.gif    document.cookie 
= name + "=" + value;// + 
InBlock.gif
    //((path == null) ? "" : ("; path=" + path)) + 
InBlock.gif
    //((domain == null) ? "" : ("; domain=" + domain)) + 
InBlock.gif
    //((secure == true) ? "; secure" : ""); 
ExpandedSubBlockEnd.gif
}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Deletecookie(name) dot.gif//删除名称为name的Cookie 
InBlock.gif
    var exp = new Date(); 
InBlock.gif    exp.setTime (exp.getTime() 
- 1); 
InBlock.gif    
var cval = GetCookie (name); 
InBlock.gif    document.cookie 
= name + "=" + cval + "; expires=" + exp.toGMTString(); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
function Clearcookie() //清除COOKIE 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif    
var temp=document.cookie.split(";"); 
InBlock.gif    
var loop3; 
InBlock.gif    
var ts; 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
for (loop3=0;loop3<temp.length;loop++dot.gif
InBlock.gif        ts
=temp[loop3].split("=")[0]; 
InBlock.gif        
if (ts.indexOf('mycat')!=-1
InBlock.gif        DeleteCookie(ts); 
//如果ts含“mycat”则执行清除 
ExpandedSubBlockEnd.gif
    }
 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function getCookieVal(offset) dot.gif//取得项名称为offset的cookie值 
InBlock.gif
    var endstr = document.cookie.indexOf (";", offset); 
InBlock.gif    
if (endstr == -1
InBlock.gif    endstr 
= document.cookie.length; 
InBlock.gif    
return unescape(document.cookie.substring(offset, endstr)); 
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
function GetCookie(name) dot.gif//取得名称为name的cookie值 
InBlock.gif
    var arg = name + "="
InBlock.gif    
var alen = arg.length; 
InBlock.gif    
var clen = document.cookie.length; 
InBlock.gif    
var i = 0
ExpandedSubBlockStart.gifContractedSubBlock.gif    
while (i < clen) dot.gif
InBlock.gif        
var j = i + alen; 
InBlock.gif        
if (document.cookie.substring(i, j) == arg) 
InBlock.gif        
return getCookieVal (j); 
InBlock.gif        i 
= document.cookie.indexOf(" ", i) + 1
InBlock.gif        
if (i == 0break
ExpandedSubBlockEnd.gif    }
 
InBlock.gif    
return null
ExpandedSubBlockEnd.gif}
 
InBlock.gifSetcookie(
"test","value!!!!!!!!");
InBlock.gifalert(GetCookie(
"test"));
InBlock.gif
InBlock.gif
InBlock.gif
function cc() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* check for a cookie */ 
InBlock.gif    
if (document.cookie == "")  
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* if a cookie is not found - alert user - 
ExpandedSubBlockEnd.gif        change cookieexists field value to false 
*/
 
InBlock.gif        alert(
"COOKIES need to be enabled!");  
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* If the user has Cookies disabled an alert will let him know  
ExpandedSubBlockEnd.gif        that cookies need to be enabled to log on.
*/
  
InBlock.gif        document.Form1.cookieexists.value 
="false"  
ExpandedSubBlockStart.gifContractedSubBlock.gif    }
 else dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//* this sets the value to true and nothing else will happen, 
ExpandedSubBlockEnd.gif        the user will be able to log on
*/
 
InBlock.gif        document.Form1.cookieexists.value 
="true" 
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif}
 
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* Set a cookie to be sure that one exists. 
ExpandedSubBlockEnd.gifNote that this is outside the function
*/
 
InBlock.gifdocument.cookie 
= 'killme' + escape('nothing') 
ExpandedBlockEnd.gif
None.gif
</ script >
None.gif
< BODY  onbeforeunload ="RunOnBeforeUnload()"  id ="Posts" >
None.gif
< FORM  Name ="Form1"  id ="Form1" >
None.gif  
< input  type ="text"  name ="cookieexists"  value ="false" >
None.gif
</ FORM >  
None.gif
< href ="test.html" > 中微家校通 </ a >
None.gif
None.gif
</ BODY >
None.gif
</ HTML >
ExpandedBlockStart.gifContractedBlock.gif
< SCRIPT  LANGUAGE ="JavaScript" > dot.gif
InBlock.gif
<!--
InBlock.gifg_blnCheckUnload 
= true;
InBlock.gif
function RunOnBeforeUnload() 
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
if (g_blnCheckUnload)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        window.event.returnValue 
= 你确是要放弃操作吗?'; 
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
//-->
None.gif
</ SCRIPT >

2.html(简单的操作cookies)
None.gif < html >
None.gif
< head >
None.gif
< title >  新文档  </ title >
None.gif
< meta  name ="Generator"  content ="EditPlus" >
None.gif
< meta  name ="Author"  content ="FlashSoft" >
None.gif
< meta  name ="Keywords"  content ="" >
None.gif
< meta  name ="Description"  content ="FlashSoft" >
None.gif
< meta  http-equiv ="Content-Type"  content ="text/html;charset=gb2312" >
None.gif
</ head >
None.gif
< body >
ExpandedBlockStart.gifContractedBlock.gif
< script  language ="JavaScript"  type ="Text/JavaScript" > dot.gif
InBlock.gif
<!--
InBlock.gif
// 实在懒得写了,为了减少字节量
InBlock.gif//
添加和获得都已经写好了,删除,你只要把时间改为-1就可以了,懒得封装了,实在无意义[代码moz兼容,值得一提,moz跟ie不是一个地方存cookie的]
ExpandedSubBlockStart.gifContractedSubBlock.gif
function Cookie()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
this.SetValue=function(name,value,hours,path,domain,secure)dot.gif{
InBlock.gif    
var str=new String();
InBlock.gif    
var nextTime=new Date();
InBlock.gif    nextTime.setHours(nextTime.getHours()
+hours);
InBlock.gif    str
=name+"="+escape(value);
InBlock.gif    
if(hours)
InBlock.gif      str
+=";expires="+nextTime.toGMTString();
InBlock.gif    
if(path)
InBlock.gif      str
+=";path="+path;
InBlock.gif    
if(domain)
InBlock.gif      str
+=";domain="+domain;
InBlock.gif    
if(secure)
InBlock.gif      str
+=";secure";
InBlock.gif    document.cookie
=str;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
this.GetValue=function(name)dot.gif{
InBlock.gif    
var rs=new RegExp("(^|)"+name+"=([^;]*)(;|$)","gi").exec(document.cookie),tmp;
InBlock.gif    
if(tmp=rs)
InBlock.gif      
return unescape(tmp[2]);
InBlock.gif    
return null;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

InBlock.gif
var _c=new Cookie();
InBlock.gif_c.SetValue(
"list1","",1);
InBlock.gif_c.SetValue(
"list2","",1);
InBlock.gifalert(_c.GetValue(
"list2"))
InBlock.gifdocument.writeln(document.cookie);
ExpandedBlockEnd.gif
//-->
None.gif
</ script >
None.gif
</ body >
None.gif
</ html >
None.gif

转载于:https://www.cnblogs.com/JimZhang/archive/2006/12/11/588997.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值