• javascript操作cookie实例


    话我就不多说了,大家都能看懂,写的有点乱,帮别人写的,贴下来,以后忘了再用
    代码实现功能:
    1.点击登录保存cookie
    2.刷新或重新打开页面读出cookie
    3.点击delete 删除当前username下的cookie

    没有完成的问题:
    大家看到在setCookie方法里能设置许多东西,如过期时间,作用域,保存路径等,但由于对这几个参数改如何设置不太清楚,比如作用域就是页面的根域名,路径该保存成什么格式的,如果有谁知道,请告诉我哈,谢谢了
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script type="text/javascript">
        
    function GetCookieVal(offset)
    //获得Cookie解码后的值
    {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr 
    = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
    }

    function SetCookie(name, value)
    //设定Cookie值
    {
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2? argv[2] : null;
    var path = (argc > 3? argv[3] : null;
    var domain = (argc > 4? argv[4] : null;
    var secure = (argc > 5? argv[5] : false;
    if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
    document.cookie 
    = name + "=" + escape (value) +((expires == null? "" : ("; expires="+ expdate.toGMTString()))
    +((path == null? "" : ("; path=" + path)) +((domain == null? "" : ("; domain=" + domain))
    +((secure == true? "; secure" : "");
    }

    function DelCookie(name)
    //删除Cookie
    {
    var exp = new Date();
    exp.setTime (exp.getTime() 
    - 1);
    var cval = GetCookie (name);
    document.cookie 
    = name + "=" + cval + "; expires="+ exp.toGMTString();
    }

    function GetCookie(name)
    //获得Cookie的原始值
    {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return GetCookieVal (j);
    = document.cookie.indexOf(" ", i) + 1;
    if (i == 0break;
    }

    return null;
    }

    function get$(obj)
    {
    return document.getElementById(obj);
    }

        
    </script>
    </head>
    <body  onload="javascript:get$('username').value=GetCookie('Page')==null?'':GetCookie('Page');get$('password').value=GetCookie(GetCookie('Page'))==null?'':GetCookie(GetCookie('Page'));">
    <table width="580" border="0" align="center">
      
    <tr>
        
    <td width="114">&nbsp;</td>
        
    <td width="456">&nbsp;</td>
      
    </tr>
      
    <tr>
        
    <td>用户名:</td>
        
    <td><input type="text" name="username" id="username"/></td>
      
    </tr>
      
    <tr>
        
    <td>密码</td>
        
    <td><input type="text" name="password" id="password" /></td>
      
    </tr>
      
    <tr>
        
    <td><input type="button" name="Submit" value="登陆" onclick="SetCookie('Page',get$('username').value);SetCookie(get$('username').value,get$('password').value);"/></td>
        
    <td><input type="button" value="delete" onclick="DelCookie(GetCookie('Page'));DelCookie('Page');get$('username').value='';get$('password').value='';"/></td>
      
    </tr>
    </table>

    </body>
    </html>


                                                       第八宗罪Tobin

  • 相关阅读:
    自调用匿名函数和js的Module模式
    设置一天中不同时段的倒计时,计算时针和分针的夹角
    移动端web开发中对点透的处理,以及理解fastclick如何做到去除300ms延迟
    使用Fiddler改变线上js文件的引用路径
    Linux下常用设置文件和文件夹读写权限操作
    RESTful API
    mysql之load语句
    Django学习之点赞功能
    Django学习之网站图标
    python学习之pyenv
  • 原文地址:https://www.cnblogs.com/tobin/p/1237999.html
Copyright © 2020-2023  润新知