其他分享
首页 > 其他分享> > js封装cookie

js封装cookie

作者:互联网

<!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=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>封装cookie</title>
    <script>
       function setCookie(name,value,iDay){
        var oDate=new Date();
        oDate.setDate(oDate.getDate()+iDay);

        document.cookie=name+'='+value+';expires='+oDate;
       }

       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){
        setCookie(name,1,-1);
       }


       setCookie('name','sdsd');
       alert(getCookie('name'));
       removeCookie('name');
    </script>
    </head>

    <body>
  
    </body>
    </html>

 

标签:function,封装,name,iDay,oDate,js,cookie,var
来源: https://www.cnblogs.com/sunnywindycloudy/p/13021394.html