A super lightweight wrapper around the Cookie method
[Example code]:
var Cookie_ = { set:function(key,val,time){//Set cookie method var date=new Date(); //Get the current time var expiresDays=time; //Set date to the time after n days date.setTime(date.getTime()+expiresDays*24*3600*1000); //Formatted as the time recognized by the cookie document.cookie=key + "=" + val +";expires="+date.toGMTString(); //设置cookie }, get:function(key){//Get cookie method /* Get cookie parameters */ var cookies = document.cookie.replace(/[ ]/g,""); //Get the cookie, and format the obtained cookie to remove space characters var arrCookie = cookies.split(";") //The obtained cookie is marked with "semicolon" and the cookie is saved in the array of arrCookie var tips; //declare variable tips for(var i=0;i<arrCookie.length;i++){ //Use the for loop to find the tips variable in the cookie var arr=arrCookie[i].split("="); //Identifies a single cookie with an "equal sign" and saves a single cookie as an arr array if(key==arr[0]){ //Match the variable name, where arr[0] refers to the cookie name, if the variable is tips, execute the assignment operation in the judgment statement tips=arr[1]; //Assign the value of the cookie to the variable tips break; // Terminate the for loop traversal } } return tips; }, del:function(key){ //Delete cookie method var date = new Date(); //Get the current time date.setTime(date.getTime()-10000); //Set date to past time document.cookie = key + "=v; expires =" +date.toGMTString();//设置cookie } };
[Test code]:
Cookie_.set('itdatacenter','001',100); console.info('itdatacenterCookie:'+Cookie_.get('itdatacenter'));
【Print result】:
itdatacenterCookie:001
Donor sharer
I didn't like programming before, but now I am an IT fan obsessed with programming. I would like to share some things I have sorted out and optimized here, hoping to help IT fans, with joy and sweat, and at the same time I also hope that everyone can support it. Of course, if you have money to support a money field (support Alipay and WeChat donations, join the it data center buckle group), but have no money to support a personal field, with your support, we will be more motivated and do better, thank you Ladies and gentlemen.