Encapsulate a cookie

let cookie = {

    / / According to the key value to get the corresponding cookie
    get:function(key){

                 //Get the cookie
        let data = document.cookie;
                 / / Get the first occurrence of the key position    pwd=
        let startIndex = data.indexOf(key+'=');
        //  name=123;pwd=abc
                 / / If the starting index value is greater than 0 means there is a cookie
        if(startIndex>-1) {

                         //The starting position of the key is equal to the position where it appears and the length of the key +1
            startIndex = startIndex+key.length+1;

                         //The end position is equal to the position after the position starting from the key; the position where the number appears

            let endIndex = data.indexOf(';',startIndex);

                         / / If the end position is not found then the end position is equal to the length of the cookie, after the content is all obtained
            endIndex = endIndex<0 ? data.length:endIndex;

            return decodeURIComponent(data.substring(startIndex,endIndex));


        }else {

            return '';
        }

    },

    set:function(key,value,time){
                 //Default save time
        let time = time;
                 / / Get the current time
        let cur = new Date();

        let undefined;

                 / / Set the specified time
        cur.setTime(cur.getTime()+time*24*3600*1000);

                 / / Create a cookie and set the life cycle to GMT time
        document.cookie = key+'='+encodeURIComponent(value)+';expires='+(time===undefined?'':cur.toGMTString());

    },

    del:function(key){

                 //Get the cookie
        let data = this.get(key);

                 / / If you get a cookie, reset the cookie life cycle to the past time
        if(data!==false){

            this.set(key,data,-1);

        }

    }

};
Copy code

Intelligent Recommendation

Mistaken by anti-virus software, Mozilla pauses Firefox 65 update

Development only four years to write business code, distributed high concurrency will not be a programmer?   After discovering some false positives in anti-virus products, Mozilla decided to...

Anonymous lambda functions in Python

Anonymous lambda functions in Python The basic format Wherein: the colon is the separator. is a function of the parameters before the colon, both X1, X2 ... Xn. colon is about X1, X2 ... Xn is express...

Use front-end background management template library Admin-LTE

Use front-end background management template library Admin-LTE Use front-end background management template library Admin-LTE installation Build an environment installation Install admin-LTE, you can ...

JavaScript implementation login box small eyes

1.html page 2.JS achieve   Reproduced in: https: //my.oschina.net/u/4107179/blog/3030562...

More Recommendation

6.9

Test Results: Knowledge points: call include<math.h> and for...

BufferedReader

Output: Use StringBuffer to store the read string Output: qwgddhtr,as;DRJOG64161SBHZRTUJ5654.6+ From the file:...

View the file size du -sh in the directory

View the file size du -sh in the directory...

C ++ subject array

Object array The concept of readers who have learned C language should be familiar with the concept of array. The elements of the array can be an int type variable, such as int array[128]; That is to ...

CocoaPods installation and use

*CocoaPods installation 1. Replace ruby ​​source 1.1 CocoaPods is based on the ruby ​​ecosystem and requires a ruby ​​environment. Use ruby ​​gem commands. So our system must have a ruby ​​environment...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top