JavaScript:js-cookie存取

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
<meta name="keywords" content="">
<meta name="description" content="">
</head>
<style>
    #btn{font-size:50px;}
    form{width:300px;margin:10px auto;display: none;}
</style>
<body>
    <a href="javascript: " id="btn">登录</a>
    <form method="" action="denglu2.html">
        <div>账号:<input type="text" name="" value="" id="zh"></div>
        <div>密码:<input type="password" name="" value="" id="mm"></div>
        <div>记住密码:<input type="checkbox" name="" value="" id="jz"></div>
        <div><input type="submit" name="" value="登录" id="dl"></div>
    </form>

</body>
<script>
function setcookie(key,value,days){//设置cookie
        var dates=new Date();
        dates.setDate(dates.getDate()+days);
        document.cookie=key+'='+escape(value)+'; expires='+dates;
    }
    function getcookie(key){//读取cookie
        var arr=unescape(document.cookie).split('; ');
        for (var i = 0; i < arr.length; i++) {
            var arr2=arr[i].split('=');
            if (arr2[0]==key) {
                return arr2[1];
            };
        };
        return false;
    }
    function rmcookie(key){//删除cookie
        setcookie(key,'1',-2);
    }

var btn=document.getElementById('btn');
var fom=document.getElementsByTagName('form')[0];
var zh=document.getElementById('zh');
var mm=document.getElementById('mm');
var jz=document.getElementById('jz');
var dl=document.getElementById('dl');

btn.οnclick=function(){
    fom.style.display='block';
}

if (getcookie('pass')) {
    zh.value=getcookie('user');
    mm.value=getcookie('pass');
    jz.checked=true;
};

dl.οnclick=function(){
    var dates=new Date();
    var riqi=(dates.getMonth()+1)+'月'+dates.getDate()+'日';
    setcookie('time',riqi,7);
    setcookie('user',zh.value,7);
    if (jz.checked) {
        setcookie('pass',mm.value,7);
    }else{
        rmcookie('pass');
    };
    alert('登录成功');
}

</script>
</html>

学习交流群617986737

转载于:https://my.oschina.net/u/3388416/blog/868343

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值