关于密码账号缓冲

1.在很多时候用户都需要登录某一个页面时能够有一个保存账号密码的选项来能让自己下一次不用再输入一遍,以下给大家 介绍一种使用cookie来实现的程序。

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src='jquery-1.7.2.js'></script>
    <script src="cookie.js"></script>
 <body>
    <h3>账号</h3>
    <input type="text" id='word'>
    <h3>密码</h3>
    <input type="text" id='pass'><br>
    <input type="checkbox" id='ck'><span>记住密码</span>
</body>
<script>
    $(function(){
        //在加载的时候调用
        $('#word').val(getCookie('word'));
        $('#pass').val(getCookie('pass'));
        $('#ck').on('click',function(){
            //设置cookie
            setCookie('word',$('#word').val())
            setCookie('pass',$('#pass').val())
        })
    })
</script>
</html>

2其中我使用了自己封装的cookie.js,熟悉jquery的同学可以去下载jquery的cookiejs.

function setCookie(name,value,iDay){
    if(iDay){
        var oDate = new Date();
        oDate.setDate(oDate.getDate()+iDay);
        document.cookie = name+'='+value+';path=/;expires='+oDate;
    }else{
        document.cookie = name+'='+value+';path=/';
    }
}

function getCookie(name){
    var arr = document.cookie.split('; ');
    for(var i=0; i<arr.length; i++){
        var arr2 = arr[i].split('=');
        if(arr2[0] == name){
            return arr2[1];
        }
    }
}

function removeCookie(name){
    setCookie(name,'',-10);
}

 

posted @ 2017-01-03 17:44  Jqpsmall白  阅读(190)  评论(0)    收藏  举报