JavaScript and cookie problem detailed _javascript tips

Source: Internet
Author: User

The original use of JS to read and write cookies has not paid attention to a problem:
The same key value, different domain (locahost.dev.xxx.com, dev.xxx.com, xxx.com, etc.) can exist in the cookie at the same time, Document.cookie can read these cookies, But there is no domain information. I also try to find out what method can be used to read the domain information of cookies, but unfortunately, did not find (do not know if you have any way to read the domain information, if so, please enlighten)
The scenario in which this problem occurs:
In the beginning, I wanted to let the cookies of the local (localhost.dev.xxx.com) and Dev (dev.xxx.com) and Uat (xxx.com) environments not affect each other, and I location.hostname generate cookied Omain

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 (.);
In writing cookies, I set domain as the cookiedomain, so that different bad cookies would be written to different domain, seemingly unrelated.
But at the time of taking, you can take out n the same key cookie value! And I only take the first occurrence of the cookie, which causes the value of the removal is likely to be wrong. On this issue, the client unit like Dogskin plaster stuck to me! explained to them N times, said that you only provide a Web site, the browser's computer does not have a value error (because only one domain). But every bug in the tooth rollup, always put this problem listed! All explanations are equal to swine.
Wood folding, then I will write all the cookies with JS write to the root domain name, save this gang call to go, big problem not concern, small problem see thief, put the cart before the horse!

Copy Code code as follows:

(function () {

Purge old versions of cookies
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 = = ";p ath=" + path;
if (domain) cookie = + ";d omain=" + domain;
if (secure) cookie = "; secure";
Document.cookie = cookie;
}

/*
It used to take all the cookies out and put them in an object, get them directly from the object, and now think about it, that's not true. Because if a cookie expires, that object is not 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]): "";
}
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);
}
}
})($. Cookies);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.