Home >Web Front-end >JS Tutorial >Detailed explanation of javascript and cookie issues_javascript skills

Detailed explanation of javascript and cookie issues_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:16:22882browse

It turned out that I never paid attention to a problem when using js to read and write cookies:
With the same key value, different domains (locahost.dev.xxx.com, dev.xxx.com, xxx.com, etc.) can exist at the same time. Regarding Cookie, document.cookie can read out all these cookies, but there is no domain information. I also tried to find a way to read out the domain information of the cookie, but unfortunately, I couldn't find it (I don't know) Do you have any way to read out the domain information? If so, please enlighten me)
The scenario where this problem occurs:
At the beginning, I wanted to make the local (localhost.dev.xxx.com) and The cookies in the dev (dev.xxx.com) and uat (xxx.com) environments do not affect each other. I generated cookieDomain

based on location.hostname.

1 var cookieDomain = document.domain; 2 3 var tmp = location.hostname.split(.); 4 5 if(tmp.length > 2)6 7 cookieDomain = tmp.slice(1).join(. );
When writing cookies, I set the domain to this cookieDomain. In this way, cookies in different environments will be written to different domains, seemingly not affecting each other.
But when fetching At this time, you can take out N cookie values ​​​​with the same key! And I only take the cookie that appears for the first time, so the value taken out is likely to be wrong. On this issue, the customer unit is like a dog-skin plaster. Stop me! I explained to them N times, saying that you only provide one URL to the outside world, and the visitor’s computer will not have a value error (because there is only one domain). But every time in the bug summary of Ya, there will always be Let’s list this problem! All explanations are tantamount to playing the piano.
Mu Zhe, then I will write all the cookies written in js to the root domain name, so as to save these guys from calling you back and forth. Don’t pay attention to big problems, but focus on small ones. Tiny thief, putting the cart before the horse!

Copy the code The code is as follows:

(function(){

    // 清除旧版本的 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++){
tmps.shift();
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;
else
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;
}

/*<以> In the past, all the cookies were taken out and put them in one object. When get, take it directly from that object. Now think about it, it is not correct. Because if the expiration time of a cookie has passed, that object has not been updated.
*/
$.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]) : "";
        }
                                                                                                                         $.getExpires(new Date().getFullYear() - 1), path, domain);
}
(;);
var t;
for (var i = 0; i < cks.length; i ) {
$.remove(cks[i].split(=)[0]. trim(), path, domain);
                                                                                                                                                                       

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn