JS Wrapper cookie Operation function instance (set, read, delete)

Source: Internet
Author: User
Tags setcookie

This paper describes the JS wrapper cookie operation function. Share to everyone for your reference, as follows:

?
123456789Ten One A - - the - - - + - + A at - - - - - in /*设置cookie*/function setCookie(name, value, iDay){  var oDate=new Date();  oDate.setDate(oDate.getDate()+iDay);  document.cookie=name+‘=‘+value+‘;expires=‘+oDate;};/*使用方法:setCookie(‘user‘, ‘simon‘, 11);*//*获取cookie*/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 ‘‘; //没找到就返回空  }};/*使用方法:getCookie(‘user‘)*//*删除cookie*/function removeCookie(name){  setCookie(name, 1, -1); //-1就是告诉系统已经过期,系统就会立刻去删除cookie};/*使用方法:removeCookie(‘user‘)*/

JS Wrapper cookie Operation function instance (set, read, delete)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.