• 创建一个欢迎 cookie 利用用户在提示框中输入的数据创建一个 JavaScript Cookie,当该用户再次访问该页面时,根据 cookie 中的信息发出欢迎信息。


    创建一个欢迎 cookie
    利用用户在提示框中输入的数据创建一个 JavaScript Cookie,当该用户再次访问该页面时,根据 cookie 中的信息发出欢迎信息。
    <html>
    <head>
    <script type="text/javascript">
    function getCookie(c_name)
    {
    if (document.cookie.length>0)
    { 
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
    }
    return ""
    }
    
    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
    }
    
    function checkCookie()
    {
    username=getCookie('username')
    if (username!=null && username!="")
      {alert('Welcome again '+username+'!')}
    else 
      {
      username=prompt('Please enter your name:',"")
      if (username!=null && username!="")
        {
        setCookie('username',username,365)
        }
      }
    }
    </script>
    </head>
    <body onLoad="checkCookie()">
    </body>
    </html>

    什么是cookie?

    cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

    有关cookie的例子:

    名字 cookie
    当访问者首次访问页面时,他或她也许会填写他/她们的名字。名字会存储于 cookie 中。当访问者再次访问网站时,他们会收到类似 "Welcome John Doe!" 的欢迎词。而名字则是从 cookie 中取回的。
    密码 cookie
    当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于 cookie 中。当他们再次访问网站时,密码就会从 cookie 中取回。
    日期 cookie
    当访问者首次访问你的网站时,当前的日期可存储于 cookie 中。当他们再次访问网站时,他们会收到类似这样的一条消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是从 cookie 中取回的。
  • 相关阅读:
    pgfplots画二维图真的很方便,多例比较
    LaTeX技巧206:使用gather输入多行公式的技巧
    LaTeX技巧205:使用split输入多行公式技巧
    LaTeX技巧207:使用align环境输入多行公式的技巧
    LaTeX技巧24:LaTeX常用命令集锦
    CTEX
    Latex常用指令学习
    LATEX数学公式基本语法
    LaTeX使用技巧
    C 命令行参数
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9855645.html
Copyright © 2020-2023  润新知