[BOM programming] — cookie assignment, expiration time, path path, encapsulation, and Storage

Time:2019-10-23

cookieIt is not the property under the window, but the property under the document. Cookie has a set of special values and assignment methods, which are different from localStorage and sessionStorage

features

  1. The cookie is under the document
  2. Cookies can be set a time to automatically clear the cache, if the cookie is not set to clear the time, then turn off the browser automatic clear.
  3. Cookie it can cross pages,But not across path paths (subpaths can be taken to parent paths)The premise is co-domain

The assignment

  • Create a new folder and create an HTML page under the foldercookie3.htmlAnd a folder called path2
  • Create a new path2 foldercookie4.html
  • Take a look at cookies:

[BOM programming] -- cookie assignment, expiration time, path path, encapsulation, and Storage

  • The assignment

    // normal assignment
    document.cookie = 'userName=Aihh' 
    
    Clear cookie with expiration time of 10s
    var d = new Date();
    d.setTime(Date.now() + 10*1000);
    document.cookie = 'userName=Aihh;Expires=' + d.toUTCString(); 
    
    // with access path
    document.cookie="age=18;path=/path2";

    Pay attention to

    • The expiration time is set in zone 0, while our Beijing time is zone +8toUTCString()Time to convert to a standard time zone;
    • path=/path2The setting is only accessible under the path path2 (if the same domain), if sopath=/Is the root path, any co-domain page can be accessed.
  • After the assignment, open the Application to view the cookie

    • Ordinary assignment

      [BOM programming] -- cookie assignment, expiration time, path path, encapsulation, and Storage

    • With expiration time, set expiration after 10s

      [BOM programming] -- cookie assignment, expiration time, path path, encapsulation, and Storage

    • With path, only pages under path2 can be accessed (if they are in the same domain)

      • That’s when it was discoveredcookie3.htmlThe cookie cannot be found

        [BOM programming] -- cookie assignment, expiration time, path path, encapsulation, and Storage

      • Open the one below path2cookie4.html, you’ll see this cookie

        [BOM programming] -- cookie assignment, expiration time, path path, encapsulation, and Storage

encapsulation

Cookie properties and values are used=Connection between properties;Partition, according to this feature of packaging

var CookieHelper = {
    addCookie: function (cookieName, cookieValue, exMinutes, cookiePath) {
        // third/fourth parameters, pass as needed
        var str = cookieName + "=" + cookieValue;
        if (arguments.length == 3) {
            let d = new Date();
            D.s etTime (Date. Now () + exMinutes * 60 * 1000); // expiration units are set to minutes
            str += ";Expires=" + d.toUTCString();
        }
        if (arguments.length == 4) {
            str += ";path=" + cookiePath;
        }
        document.cookie = str;
    },
    getCookie: function (cookieName) {
        var cookieArr = document.cookie.split(";");
        for (let i in cookieArr) {
            var arr = cookieArr[i].split("=");
            if (arr[0].trim() == cookieName) {
                return arr[1];
            }
        }
    },
    removeCookie: function (cookieName) {
        document.cookie = cookieName + "= ;Expires=" + (new Date()).toUTCString();
    }
}

And localStorage, sessionStorage

  1. LocalStorage, sessionStorage is under the window, cookie is under the document
  2. LocalStorage to manually clear, sessionStorage turn off the browser automatic clear, and cookie can set a time to automatically clear,cookie if not set clear time, then turn off the browser automatic clear.
  3. LocalStorage can span co-domain pages, sessionStorage can only span parent and child pages (viaopen()It can span pages, but not paths (subpaths can be taken to parent paths).They can’t cross domains

Recommended Today

Database lock (notes)

Introduction MyISAMTransaction is not supported. What is the difference between MyISAM and InnoDB in terms of locks MyISAMTable level lock is used by default, row level lock is not supported. InnoDBRow level locks are used by default, and table level locks are also supported. Read lock / shared lock: othersessionCan read, but can’t write. Write […]