CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
(图)邪恶的韩国UMPC 使用 Java 编写数据库应用新规范
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  JavaScript

JavaScript能用cookie吗?给我详细的使用方法!多谢!!!

楼主Ailong(专家分好难挣啊)2001-05-22 17:31:00 在 Web 开发 / JavaScript 提问

问题点数:80、回复次数:5Top

1 楼net_lover(【孟子E章】)回复于 2001-05-22 17:40:00 得分 80

能啊!!Top

2 楼net_lover(【孟子E章】)回复于 2001-05-22 18:01:00 得分 0

cookie   Property  
   
  --------------------------------------------------------------------------------  
   
  Sets   or   retrieves   the   string   value   of   a   cookie.  
   
  Syntax  
   
  HTML   N/A    
  Scripting   document.cookie   [   =   sCookie   ]    
   
  Possible   Values  
   
  sCookie   String   that   specifies   or   receives   the   name=value;   pairs,   plus   any   of   the   values   listed   in   Possible   Values.   expires=date;   Setting   no   expiration   date   on   a   cookie   causes   it   to   expire   when   the   browser   closes.   If   you   set   an   expiration   date   in   the   future,   the   cookie   is   saved   across   browser   sessions.   If   you   set   an   expiration   date   in   the   past,   the   cookie   is   deleted.   Use   GMT   format   to   specify   the   date.      
  domain=domainname;   Setting   the   domain   of   the   cookie   allows   pages   on   a   domain   made   up   of   more   than   one   server   to   share   cookie   information.      
  path=path;   Setting   a   path   for   the   cookie   allows   the   current   document   to   share   cookie   information   with   other   pages   within   the   same   domain—that   is,   if   the   path   is   set   to   /thispathname,   all   pages   in   /thispathname   and   all   pages   in   subfolders   of   /thispathname   can   access   the   same   cookie   information.      
  secure;   Setting   a   cookie   as   secure;   means   the   stored   cookie   information   can   be   accessed   only   from   a   secure   environment.    
     
   
  The   property   is   read/write.   The   property   has   no   default   value.  
   
  Expressions   can   be   used   in   place   of   the   preceding   value(s),   as   of   Microsoft®   Internet   Explorer   5.   For   more   information,   see   Dynamic   Properties.  
   
  Remarks  
   
  A   cookie   is   a   small   piece   of   information   stored   by   the   browser.   Each   cookie   is   stored   in   a   name=value;   pair   called   a   crumb—that   is,   if   the   cookie   name   is   "id"   and   you   want   to   save   the   id's   value   as   "this",   the   cookie   would   be   saved   as   id=this.   You   can   store   up   to   20   name=value   pairs   in   a   cookie,   and   the   cookie   is   always   returned   as   a   string   of   all   the   cookies   that   apply   to   the   page.   This   means   that   you   must   parse   the   string   returned   to   find   the   values   of   individual   cookies.  
   
  Cookies   accumulate   each   time   the   property   is   set.   If   you   try   to   set   more   than   one   cookie   with   a   single   call   to   the   property,   only   the   first   cookie   in   the   list   will   be   retained.  
   
  You   can   use   the   Microsoft®   JScript®   split   method   to   extract   a   value   stored   in   a   cookie.    
   
  Examples  
   
  This   example   creates   a   cookie   with   a   specified   name   and   value.   The   value   is   passed   to   the   JScript   escape   function   to   ensure   that   the   value   only   contains   valid   characters.   When   the   cookie   is   retrieved,   the   JScript   unescape   function   should   be   used   to   translate   the   value   back   to   its   original   form.  
   
  <SCRIPT>  
  //   Create   a   cookie   with   the   specified   name   and   value.  
  //   The   cookie   expires   at   the   end   of   the   20th   century.  
  function   SetCookie(sName,   sValue)  
  {  
      date   =   new   Date();  
      document.cookie   =   sName   +   "="   +   escape(sValue)   +   ";   expires="   +   date.toGMTString();  
  }  
  </SCRIPT>  
  This   example   retrieves   the   value   of   the   portion   of   the   cookie   specified   by   the   sCookie   parameter.  
   
  Show   Example  
  <SCRIPT>  
  //   Retrieve   the   value   of   the   cookie   with   the   specified   name.  
  function   GetCookie(sName)  
  {  
      //   cookies   are   separated   by   semicolons  
      var   aCookie   =   document.cookie.split(";   ");  
      for   (var   i=0;   i   <   aCookie.length;   i++)  
      {  
          //   a   name/value   pair   (a   crumb)   is   separated   by   an   equal   sign  
          var   aCrumb   =   aCookie[i].split("=");  
          if   (sName   ==   aCrumb[0])    
              return   unescape(aCrumb[1]);  
      }  
   
      //   a   cookie   with   the   requested   name   does   not   exist  
      return   null;  
  }  
  </SCRIPT>  
  This   example   deletes   a   cookie   by   setting   its   expires   attribute   to   a   past   date.   A   cookie   deleted   in   this   manner   might   not   be   removed   immediately   by   the   browser.  
   
  <SCRIPT>  
  //   Delete   the   cookie   with   the   specified   name.  
  function   DelCookie(sName)  
  {  
      document.cookie   =   sName   +   "="   +   escape(sValue)   +   ";   expires=Fri,   31   Dec   1999   23:59:59   GMT;";  
  }  
  </SCRIPT>  
  Top

3 楼net_lover(【孟子E章】)回复于 2001-05-22 18:03:00 得分 0

http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/cookie.aspTop

4 楼net_lover(【孟子E章】)回复于 2001-05-22 18:26:00 得分 0

http://developer.netscape.com/docs/manuals/communicator/jsguide4/cookies.htm#1002170Top

5 楼net_lover(【孟子E章】)回复于 2001-05-22 18:27:00 得分 0

 
  Using   Cookies  
  Netscape   cookies   are   a   mechanism   for   storing   persistent   data   on   the   client   in   a   file   called   cookies.txt.   Because   HyperText   Transport   Protocol   (HTTP)   is   a   stateless   protocol,   cookies   provide   a   way   to   maintain   information   between   client   requests.   This   section   discusses   basic   uses   of   cookies   and   illustrates   with   a   simple   example.   For   a   complete   description   of   cookies,   see   Appendix   C,   "Netscape   Cookies.".  
  Each   cookie   is   a   small   item   of   information   with   an   optional   expiration   date   and   is   added   to   the   cookie   file   in   the   following   format:  
   
   
  name=value;expires=expDate;  
  name   is   the   name   of   the   datum   being   stored,   and   value   is   its   value.   If   name   and   value   contain   any   semicolon,   comma,   or   blank   (space)   characters,   you   must   use   the   escape   function   to   encode   them   and   the   unescape   function   to   decode   them.  
  expDate   is   the   expiration   date,   in   GMT   date   format:  
   
   
  Wdy,   DD-Mon-YY   HH:MM:SS   GMT  
  Although   it's   slightly   different   from   this   format,   the   date   string   returned   by   the   Date   method   toGMTString   can   be   used   to   set   cookie   expiration   dates.  
  The   expiration   date   is   an   optional   parameter   indicating   how   long   to   maintain   the   cookie.   If   expDate   is   not   specified,   the   cookie   expires   when   the   user   exits   the   current   Navigator   session.   Navigator   maintains   and   retrieves   a   cookie   only   if   its   expiration   date   has   not   yet   passed.    
   
  For   more   information   on   escape   and   unescape,   see   the   JavaScript   Reference.  
   
   
  Limitations  
  Cookies   have   these   limitations:  
   
   
  300   total   cookies   in   the   cookie   file.    
   
  4   Kbytes   per   cookie,   for   the   sum   of   both   the   cookie's   name   and   value.    
   
  20   cookies   per   server   or   domain   (completely   specified   hosts   and   domains   are   treated   as   separate   entities   and   have   a   20-cookie   limitation   for   each,   not   combined).    
  Cookies   can   be   associated   with   one   or   more   directories.   If   your   files   are   all   in   one   directory,   then   you   need   not   worry   about   this.   If   your   files   are   in   multiple   directories,   you   may   need   to   use   an   additional   path   parameter   for   each   cookie.   For   more   information,   see   Appendix   C,   "Netscape   Cookies."  
   
  Using   Cookies   with   JavaScript  
  The   document.cookie   property   is   a   string   that   contains   all   the   names   and   values   of   Navigator   cookies.   You   can   use   this   property   to   work   with   cookies   in   JavaScript.  
  Here   are   some   basic   things   you   can   do   with   cookies:  
   
   
   
  Set   a   cookie   value,   optionally   specifying   an   expiration   date.    
   
  Get   a   cookie   value,   given   the   cookie   name.    
  It   is   convenient   to   define   functions   to   perform   these   tasks.   Here,   for   example,   is   a   function   that   sets   cookie   values   and   expiration:  
   
  //   Sets   cookie   values.   Expiration   date   is   optional//function   setCookie(name,   value,   expire)   {       document.cookie   =   name   +   "="   +   escape(value)       +   ((expire   ==   null)   ?   ""   :   (";   expires="   +   expire.toGMTString()))}  
  Notice   the   use   of   escape   to   encode   special   characters   (semicolons,   commas,   spaces)   in   the   value   string.   This   function   assumes   that   cookie   names   do   not   have   any   special   characters.  
  The   following   function   returns   a   cookie   value,   given   the   name   of   the   cookie:  
   
   
  function   getCookie(Name)   {       var   search   =   Name   +   "="       if   (document.cookie.length   >   0)   {   //   if   there   are   any   cookies             offset   =   document.cookie.indexOf(search)               if   (offset   !=   -1)   {   //   if   cookie   exists                     offset   +=   search.length                     //   set   index   of   beginning   of   value                   end   =   document.cookie.indexOf(";",   offset)                     //   set   index   of   end   of   cookie   value                   if   (end   ==   -1)                           end   =   document.cookie.length                   return   unescape(document.cookie.substring(offset,   end))             }         }}  
  Notice   the   use   of   unescape   to   decode   special   characters   in   the   cookie   value.  
   
  Using   Cookies:   an   Example  
  Using   the   cookie   functions   defined   in   the   previous   section,   you   can   create   a   simple   page   users   can   fill   in   to   "register"   when   they   visit   your   page.   If   they   return   to   your   page   within   a   year,   they   will   see   a   personal   greeting.    
  You   need   to   define   one   additional   function   in   the   HEAD   of   the   document.   This   function,   register,   creates   a   cookie   with   the   name   TheCoolJavaScriptPage   and   the   value   passed   to   it   as   an   argument.  
   
   
  function   register(name)   {       var   today   =   new   Date()       var   expires   =   new   Date()       expires.setTime(today.getTime()   +   1000*60*60*24*365)       setCookie("TheCoolJavaScriptPage",   name,   expires)}  
  The   BODY   of   the   document   uses   getCookie   (defined   in   the   previous   section)   to   check   whether   the   cookie   for   TheCoolJavaScriptPage   exists   and   displays   a   greeting   if   it   does.   Then   there   is   a   form   that   calls   register   to   add   a   cookie.   The   onClick   event   handler   also   calls   history.go(0)   to   redraw   the   page.  
   
  <BODY><H1>Register   Your   Name   with   the   Cookie-Meister</H1><P><SCRIPT>var   yourname   =   getCookie("TheCoolJavaScriptPage")   if   (yourname   !=   null)       document.write("<P>Welcome   Back,   ",   yourname)else       document.write("<P>You   haven't   been   here   in   the   last   year...")</SCRIPT>  
  <P>   Enter   your   name.   When   you   return   to   this   page   within   a   year,   you   will   be   greeted   with   a   personalized   greeting.   <BR><FORM   onSubmit="return   false">Enter   your   name:   <INPUT   TYPE="text"   NAME="username"   SIZE=   10><BR><INPUT   TYPE="button"   value="Register"       onClick="register(this.form.username.value);   history.go(0)"></FORM>  
  Top

相关问题

  • 哪里有关于DivX.dll详细使用方法?多谢!
  • 哪为可以详细介绍一下commandtype属性?多谢了
  • 谁有DSE算法的详细资料,多谢
  • 请教 ADODataSet和ADOCommand 的详细用法! 急!!!多谢了!!!
  • 一道初级题,请帮忙解答,越详细越好,多谢了,多谢了。
  • 有谁知道OutLook邮箱的文件格式(详细),多谢!一定加分!
  • 想要一份 详细设计(实例)的文档作为参考。多谢!
  • 推荐一本讲属性、事件的比较详细的书?多谢啦!
  • 在哪可以下载到Oracle For Linux,请给出详细链接,多谢!
  • 请问谁知道MsChart组件的详细便用方法 多谢……

关键词

  • greeting
  • cookie
  • your name

得分解答快速导航

  • 帖主:Ailong
  • net_lover

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo