Details on javascript and cookies

  • 2020-03-27 00:01:01
  • OfStack

Js used to read and write cookies has not paid attention to a problem:
The same key value, different domains (,,, etc.) can exist in the Cookie at the same time, the document.cookie can read all these cookies, but there is no domain information. Not found (I wonder if you have any way to read the domain information, if so, please comment)
The scenario where the problem occurs:
Initially, I wanted the cookies of the local ( and dev ( environments to be independent of each other, so I generated cookieDomain based on location.hostname

Var cookieDomain = document.domain;     2, 3,   Var TMP = location. The hostname. The split (.);     4, 5   If (TMP) length > 2)6 7 cookieDomain = tmp.slice(1). Join (.);
When I wrote the cookie, I set the domain to be this cookieDomain, so that cookies from different environments would be written to different domains, seemingly independent of each other.
But when you fetch, you can fetch N cookie values of the same keys! However, I only took the cookie that appeared for the first time, which resulted in the possibility that the extracted value was wrong. In this case, the client unit stuck to me like a dog skin plaster! I explained to them N times that you only provide one url to the public, and the visitor's computer will not take the wrong value (because there is only one   Domain). But every time the teeth of the BUG summary, the problem will always be listed! All explanations are casting pearls before swine.
Wood fold, that I put all the cookies written with js to write under the root domain name, save this guy called to call, the big problem is not concerned, the small problem to see the thief, put the cart before the horse!


    //Clear the old version of the cookie
    if(CTSZ.Cookie.get("cookieVersion") != Params.cookieVersion){
        var tmps = Params.orgDomain.split(.);
        var domain;
        var len = tmps.length;
        for(var i=0;i<= len - 3; i++){
            domain = tmps.join(.);
            CTSZ.Cookie.empty("/", domain);
        CTSZ.Cookie.set("cookieVersion", Params.cookieVersion, Params.cookieExpires, "/", Params.cookieDomain);
    $.Cookie = {};
    (function ($) {
        $.getExpires = function (y, m, d, h, i, s, ms) {
            var date = new Date();
            y = isNaN(y) ? date.getFullYear() : y;
            m = isNaN(m) ? date.getMonth() : m - 1;
            d = isNaN(d) ? date.getDate() : d;
            h = isNaN(h) ? date.getHours() : h;
            i = isNaN(i) ? date.getMinutes() : i;
            s = isNaN(s) ? date.getSeconds() : s;
            ms = isNaN(ms) ? date.getMilliseconds() : ms;
            return new Date(y, m, d, h, i, s, ms).toUTCString();
        $.getExpiresByUTCString = function (UTCString) {
            var s = new Date(UTCString).toUTCString();
            if (s == NaN || s == Invalid Date)
                return null; // IE,Opera NaN , FF,Safari Invalid Date;
                return s;
        $.set = function (k, v, expires, path, domain, secure) {
            var cookie = k + = + encodeURIComponent(v);
            if (expires) cookie += ";expires=" + expires;
            if (path) cookie += ";path=" + path;
            if (domain) cookie += ";domain=" + domain;
            if (secure) cookie += ";secure";
            document.cookie = cookie;
        $.get = function (k) {
            var cks = document.cookie.split(;);
            var t;
            for (var i = 0; i < cks.length; i++) {
                t = cks[i].split(=);
                if (k == t[0].trim()) return t.length >= 2 ? decodeURIComponent(t[1]) : "";
            return null;
        $.remove = function (k, path, domain) {
            $.set(k, , $.getExpires(new Date().getFullYear() - 1), path, domain);
        $.empty = function (path, domain) {
            var cks = document.cookie.split(;);
            var t;
            for (var i = 0; i < cks.length; i++) {
                $.remove(cks[i].split(=)[0].trim(), path, domain);

Related articles: