(1) No cookie of the same name is set on the current page;
(2) the cookie with the same name has been set on the current page;
The main test code is as follows:
// A super-long string exceeding 4 K
Var longstring = 'dfsafdsafdfsafdsafdfsafdsafdsafdfsafdfsafdfs
Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs
Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsaf
Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdf
Safdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdfs
Afdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsaf
Dsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafd
Fsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa
Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsafdf
Bytes
Bytes
Bytes
Bytes
Bytes
Dfsafdsafdfsafdsafdfsafdsafdfsafdfsafdsafdfsafdsafdfsafdfsaf
Dsafdfsa
Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsa
Fdsafdfsafdsafdfsafdsafdfsafdsafdsafdfsafdsafdfsafdfsafdsafdsafdfsafdsaf
Bytes
;
// Write cookie function
Function setcookie (name, value)
{
Var days = 1; // This cookie will be saved for 1 day
Var exp = new date ();
Exp. settime (exp. gettime () + days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. togmtstring ();
}
Function getcookie (name) // The cookie function.
{
Var arr = document. cookie. match (new regexp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;
}
// Set the cookie, which contains the two situations mentioned at the beginning of the article:
Setcookie ('test', 'Cookie already set ');
// Set the ultra-long cookie:
Setcookie ('test', longstring );
Alert (getcookie ('test '));
The test results are as follows:
(1) No cookie of the same name is set on the current page;
Browser version
|
Output result
|
Ie6
|
Null
|
Ie7
|
Null
|
Ie8
|
Null
|
Firefox
|
Null
|
Chrome
|
Null
|
Opera
|
Null
|
Conclusion:
In the browsers of the above versions, writing strings larger than 4 KB (of course, different browsers may have different situations, some are 4095 bytes, some are 4096 bytes) will fail to be written,
The write truncation cannot be performed.
(2) the cookie with the same name has been set on the current page;
Browser version
|
Output result
|
Ie6
|
Null
|
Ie7
|
Null
|
Ie8
|
Cookie already set
|
Firefox
|
Cookie already set
|
Chrome
|
Cookie already set
|
Opera
|
Cookie already set
|
Conclusion:
When a value named 'test' already exists in browsers of the above versions, it is written to a string larger than 4 KB (of course, the situation may be different in different browsers, some are 4095 bytes, some are 4096 bytes:
1. In ie6 and ie7, if a super-long string cannot be written, the cookie value with the original key 'test' will be affected. In the test results, the cookie value is cleared;
2. In other browsers, the cookie value of the original same key name is not affected when the ultra-long string cannot be written. The original cookie value can still be read normally.