• cookie -- 添加删除


    前段时间学到了cookie,之前的公司用的jquery插件,现在终于学到了原生的js

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta name="author" content="智能社 - zhinengshe.com" />
    <meta name="copyright" content="智能社 - zhinengshe.com" />
    <title>智能社 - www.zhinengshe.com</title>
    <script>
    //添加
    function addCookie(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]
            }
        }
        return ''
    }
    
    
    //删除
    function removeCookie(name){
        addCookie(name,'',-1)
    }
    
    window.onload = function(){
        var oForm = document.getElementsByTagName("form")[0];
        var oUser = document.getElementsByName("user")[0];
        var oPass = document.getElementsByName("pwd")[0];
    
    
        var user = getCookie("user");
        var pwd = getCookie("pwd");
    
        oUser.value = user;
        oPass.value = pwd;
    
        oForm.onsubmit = function(){
    
            if(oUser.value=="" || oPass.value== ""){
    
                return false;
            }
    
            addCookie("user",oUser.value,7);
            addCookie("pwd",oPass.value);
            //removeCookie("user")
        };
    
    };
    
    </script>
    </head>
    
    <body>
    <form action="http://www.zhinengshe.com/">
        用户名:<input name="user" type="text" value="" /><br/>
        密  码:<input name="pwd" type="text" value="" /><br/>
        <input type="submit" />
    </form>
    </body>
    </html>
  • 相关阅读:
    [USACO 2012 Feb B]Moo
    [Atcoder ARC124] XOR Matching 2-小思维 | 暴力
    loj数列分块入门
    2019牛客暑期多校2-Partition problem深搜
    Codeforces 1554C
    [USACO 2012 Feb G]Cow Coupons----贪心&带悔(看完稳AC)
    Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
    [AtCoder ARC098] Donation| 建图 | 树型dp
    关于幂等性以及怎么实现幂等性
    【OOM】解决思路
  • 原文地址:https://www.cnblogs.com/heboliufengjie/p/4718078.html
Copyright © 2020-2023  润新知