js 封装设计cookie

http://www.imooc.com/article/12700<!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" />
<title>无标题文档</title>
<script>
function setCookie(name, value, iDay)
{
  var oDate=new Date();
  oDate.setDate(oDate.getDate()+iDay); //用来设置过期时间用的,获取当前时间加上传进来的iDay就是过期时间
  document.cookie=name+'='+value+';expires='+oDate;
};
function getCookie(name)
{
  var arr=document.cookie.split('; '); //多个cookie值是以; 分隔的,用split把cookie分割开并赋值给数组
  for(var i=0;i<arr[i].length;i++) //历遍数组
  {
    var arr2=arr[i].split('='); //原来割好的数组是:user=simon,再用split('=')分割成:user simon 这样可以通过arr2[0] arr2[1]来分别获取user和simon 
    if(arr2[0]==name) //如果数组的属性名等于传进来的name
    {
      return arr2[1]; //就返回属性名对应的值
    }
    return ''; //没找到就返回空
  }
};
function removeCookie(name)
{
  setCookie(name, 1, -1); //-1就是告诉系统已经过期,系统就会立刻去删除cookie
};
window.onload=function()
{
  var form=document.getElementById('form');
  var user=document.getElementsByName('user')[0];
  form.onsubmit=function()
  {
    setCookie('user', user.value, 14);
  };
  user.value=getCookie('user');
};
</script>
</head>
<body>
<form action="" id="form">
  用户名:<input type="text" name="user" /><br />
  密码:<input type="password" name="pass" /><br />
  <input type="submit" value="登录" />
</form>
</body>
</html>

参考::zhuhttp://www.jb51.net/article/75463.htm

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
function setCookie(name, value, iDay)
{
   var oDate= new Date();
   oDate.setDate(oDate.getDate()+iDay); //用来设置过期时间用的,获取当前时间加上传进来的iDay就是过期时间
   document.cookie=name+ '=' +value+ ';expires=' +oDate;
};
function getCookie(name)
{
   var arr=document.cookie.split( '; ' ); //多个cookie值是以; 分隔的,用split把cookie分割开并赋值给数组
   for ( var i=0;i<arr[i].length;i++) //历遍数组
   {
     var arr2=arr[i].split( '=' ); //原来割好的数组是:user=simon,再用split('=')分割成:user simon 这样可以通过arr2[0] arr2[1]来分别获取user和simon
     if (arr2[0]==name) //如果数组的属性名等于传进来的name
     {
       return arr2[1]; //就返回属性名对应的值
     }
     return '' ; //没找到就返回空
   }
};
function removeCookie(name)
{
   setCookie(name, 1, -1); //-1就是告诉系统已经过期,系统就会立刻去删除cookie
};
window.onload= function ()
{
   var form=document.getElementById( 'form' );
   var user=document.getElementsByName( 'user' )[0];
   form.onsubmit= function ()
   {
     setCookie( 'user' , user.value, 14);
   };
   user.value=getCookie( 'user' );
};
</script>
</head>
<body>
<form action= "" id= "form" >
   用户名:<input type= "text" name= "user" /><br />
   密码:<input type= "password" name= "pass" /><br />
   <input type= "submit" value= "登录" />
</form>
</body>
</html>

转载于:https://www.cnblogs.com/freddyhuang/p/5914524.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
封装 JavaScript 的 Cookie,可以创建一个名为 "CookieUtil" 的对象,并为该对象定义一些方法来处理 Cookie。以下是一个基本的示例: ```javascript var CookieUtil = { // 获取指定名称的 Cookie 值 get: function(name) { var cookieName = encodeURIComponent(name) + "=", cookieStart = document.cookie.indexOf(cookieName), cookieValue = null; if (cookieStart > -1) { var cookieEnd = document.cookie.indexOf(";", cookieStart); if (cookieEnd == -1) { cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd)); } return cookieValue; }, // 设置 Cookie set: function(name, value, expires, path, domain, secure) { var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); if (expires instanceof Date) { cookieText += "; expires=" + expires.toGMTString(); } if (path) { cookieText += "; path=" + path; } if (domain) { cookieText += "; domain=" + domain; } if (secure) { cookieText += "; secure"; } document.cookie = cookieText; }, // 删除指定名称的 Cookie remove: function(name, path, domain, secure) { this.set(name, "", new Date(0), path, domain, secure); } }; ``` 使用示例: ```javascript // 设置一个名为 "username" 的 Cookie,有效期为一天 CookieUtil.set("username", "John Doe", new Date(Date.now() + (24 * 60 * 60 * 1000))); // 获取名为 "username" 的 Cookie 值 var username = CookieUtil.get("username"); console.log(username); // 删除名为 "username" 的 Cookie CookieUtil.remove("username"); ``` 这只是一个简单的封装示例,你可以根据需要进行扩展和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值