zoukankan      html  css  js  c++  java
  • cookie的使用

    名字 cookie

    当访问者首次访问页面时,他或她也许会填写他/她们的名字。名字会存储于 cookie 中。当访问者再次访问网站时,他们会收到类似 "Welcome John Doe!" 的欢迎词。而名字则是从 cookie 中取回的。

    密码 cookie

    当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于 cookie 中。当他们再次访问网站时,密码就会从 cookie 中取回。

    日期 cookie

    当访问者首次访问你的网站时,当前的日期可存储于 cookie 中。当他们再次访问网站时,他们会收到类似这样的一条消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是从 cookie 中取回的。
     1 //创建和存储 cookie
     2             function getCookie(c_name) {
     3                 if(document.cookie.length > 0) {
     4                     c_start = document.cookie.indexOf(c_name + "=")
     5                     if(c_start != -1) {
     6                         c_start = c_start + c_name.length + 1
     7                         c_end = document.cookie.indexOf(";", c_start)
     8                         if(c_end == -1) c_end = document.cookie.length
     9                         return unescape(document.cookie.substring(c_start, c_end))
    10                     }
    11                 }
    12                 return ""
    13             }
    14             
    15             function setCookie(c_name, value, expiredays) {
    16                 var exdate = new Date()
    17                 exdate.setDate(exdate.getDate() + expiredays)
    18                 document.cookie = c_name + "=" + escape(value) +
    19                     ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
    20             }
    21 
    22             function checkCookie() {
    23                 username = getCookie('username')
    24                 if(username != null && username != "") {
    25                     alert('Welcome again ' + username + '!')
    26                 } else {
    27                     username = prompt('Please enter your name:', "")
    28                     if(username != null && username != "") {
    29                         setCookie('username', username, 365)
    30                     }
    31                 }
    32             }
  • 相关阅读:
    几种常用的排序算法
    Charles 抓包工具安装和采坑记录
    当你骂特朗普的时候你究竟在骂什么
    苹果公司的另一面:沃兹尼亚克
    网络爬虫设计中需要注意的几个问题
    微信小程序 canvas 绘图问题总结
    自己动手做智能家居之:智能空调控制
    Allegro导入PADS文件
    C#
    C#
  • 原文地址:https://www.cnblogs.com/hgs-159/p/6795246.html
Copyright © 2011-2022 走看看