How to realize the communication between multiple tabs in the same browser (2) -- cookie+setInterval

Keywords: Javascript Programming SSL

Two, cookie
(1) What is a cookie?
HTTP cookies, often called cookies directly, were originally used by clients to store callback information. The standard requires the server to send a Set-CookieHTTP header to any HTTP request as a corresponding part, which contains the callback information. The browser stores such reply information, and after that, sends the information back to the server by adding a cookie HTTP header to each request.
(2) How do I use cookie s?
In JavaScript, the interface of cookie, document.cookie, is not very friendly. You can encapsulate the corresponding interface yourself.
Basic cookie operations: read, write, delete

//The code comes from JavaScript advanced programming
var CookieUtil={
            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;
            },
            set:function(name,value,expires,path,domain,secure){//Cookie composition: name, value, expiration time (when to stop sending cookies to the browser), path (path to the specific domain where the cookie is sent to the server), domain (for which domain the cookie is valid), security flag (after specified, it can only be sent to the server when SSL connection is used)
                var cookieText=encodeURIComponent(name) + "=" +encodeURIComponent(value);
                if(expires instanceof Date){
                    cookieText += "; expires="+expires.toGMTString();//The time is in the format of GMT. please note that ";" is used to divide the information
                }
                if(path){
                    cookieText += "; path="+path;
                }
                if(domain){
                    cookieText += "; domain="+domain;
                }
                if(secure){
                    cookieText += "; secure";
                }

                document.cookie = cookieText;
            },
            unset:function(name,path,domain,secure){//There is no way to delete cookie s directly
                this.set(name,"",new Date(0),path,domain,secure);//Use the same path, domain, security options to set the cookie again, and set the expiration time to the past time
            }
        };

Use:

        //Set cookie
        CookieUtil.set("name",'lwf');
        CookieUtil.set("age",21);

        // Read cookie
        console.log(CookieUtil.get("name"));
        console.log(CookieUtil.get("age"));

        // delete cookie
        CookieUtil.unset("name");
        CookieUtil.unset("age");

To be continued...

Posted by elenev on Mon, 30 Mar 2020 22:32:31 -0700