Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
在JAVASCRIPT中怎么使用COOKIE?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chen Liqiang  
View profile  
 More options Aug 29 2006, 9:12 pm
From: "Chen Liqiang" <chen...@gmail.com>
Date: Tue, 29 Aug 2006 18:12:04 -0700
Local: Tues, Aug 29 2006 9:12 pm
Subject: 在JAVASCRIPT中怎么使用COOKIE?

作者:hareqiqi

在JAVASCRIPT中怎么使用COOKIE?
database
---------------------------------------------------------------

document.cookie
---------------------------------------------------------------

http://expert.csdn.net/Expert/TopicView2.asp?id=750278&datebasetype=now

---------------------------------------------------------------

读取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;

}

alert(  readCookie("myCookie")  );

写入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;

}

writeCookie("myCookie",  "my  name",  24);

---------------------------------------------------------------

+  号字符串连接符号

至于cookies,请看:

用  COOKIES  记录一个人访问次数的小程序。

<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>

---------------------------------------------------------------

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();

}

function  getCookie(name)
{
     var  arr,reg=new  RegExp("(^  |  )"+name+"=([^;]*)(;  |$)");
             if(arr=document.cookie.match(reg))  return
unescape(arr[2]);
             else  return  null;
}

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();
}

---------------------------------------------------------------

http://www.hkln.net/js/advance/cookie.htm
---------------------------------------------------------------

读写cookie的时候最好用escape  和unescape
---------------------------------------------------------------

浏览器地址栏打:  

javascript:alert(document.cookie);


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google