dom cookie记录用户名

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function ()
{
    var ologin = document.getElementById('login');
    var odel = document.getElementById('del');
    var ousername = document.getElementById('username');
    
    if(getcookie('username'))
    {
        ousername = getcookie('username');
    }
    
    ologin.onclick = function()
    {
        alert('登陆成功')
        setcookie('username',ousername.value,5);
    }
    
    
    
    odel.onclick = function ()
    {
        removecookie('username');
        ousername.value = '';
        
        
    }
    
    function getcookie (key)
    {
        var arr1 = document.cookie.split(';');
        for(var i = 0; i < arr1.length; i++)
        {
            var arr2 = arr1[i].split('=');
            if(key == arr2[0])
            {
                return decodeURI(arr2[1]);
            }
        }
    }
    
    
    function getcookie (key)
    {
        var arr1 = document.cookie.split(';');
        for(var i = 0; i < arr1.length; i++)
        {
            var arr2 = arr1[i].split('=');
            if(key == arr2[0])
            {
                return decodeURI(arr2[1]);
            }
        }
    }
    
    function setcookie(key,value,t)
    {
        var oDate = new Date();
        oDate.setDate(oDate.getDate()+t);
        document.cookie = key + ':' + value + ';expires =' + oDate.toGMTString();
    }
    
    
    function removecookie(key)
    {
        setcookie(key,'',-1);
    }
};
</script>
</head>

<body>
<input type="text" id="username">
<input type="button" id="login" value="登陆">
<input type="button" id="del" value="删除">
</body>
</html>
原文地址:https://www.cnblogs.com/mayufo/p/4243823.html