A brief analysis of javascript operation cookie objects

  • 2020-05-09 18:07:11
  • OfStack

Cookie object

Is a type of data information (Cookie data) stored in the Cookies folder on the client hard drive in the form of a file (Cookie file). User data information in the Cookie folder (Cookie data). The Cookie file is created by the visited Web site to store the session data between the client and the Web site for a long time, and the Cookie data is only allowed to be read by the visited Web site. (cross-domain access is not allowed)

Cookie file format:

NS:Cookie.txt
IE: username @domain.txt

Write Cookie javascript

Format:
document.cookie= "keyword = value [;expires= expiry date][;...] "
Remark:
Valid date format :Wdy, DD-Mon-YY HH:MM: SS
Wdy/Mon: English week/month;
Also contains path, domain, secure attributes;
Each Web site (domain) can set up 20 Cookie data;
Each browser can store 300 Cookie data, 4K bytes;
The client has disabled the writing of Cookie data.

Operating Cookie with js is more cumbersome than operating Cookie with jsp servlet

Why are Cookie files for Session not visible in the Cookie folder?

          USES two types of Cookie

Persistence Cookie, which is stored on the client's hard drive.
Session Cookie: it is not stored on the client's hard drive, but in the memory of the browser process. When the browser is closed, the session Cookie is destroyed.


<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
    var today=new Date();
   
    var expireDay=new Date();
    var msPerMonth=24*60*60*1000*31;
    expireDay.setTime(today.getTime()+msPerMonth);
    document.cookie="name=liujl;expires="+expireDay.toGMTString();
    document.writeln("cookie It's written to the hard drive ");
    document.writeln(" The content is :"+document.cookie);
    document.writeln(" Expiration time :"+expireDay.toGMTString());
</script>
</body>
</html>


Related articles: