How to obtain the cookie expiration time using javascript
Javascript and dynamic pages cannot get the cookie expiration time. The expiration time is managed by the browser. javascript and dynamic pages can only set the expiration time, but cannot pass the document. cookie (javascript) or Cookie. the Expires (asp.net) attribute is obtained.
The Code is as follows:
<% @ Page language = "C #" Debug = "true" %>
<Script runat = "server">
Protected void Page_Load (object sender, EventArgs e)
{
HttpCookie hc = Request. Cookies ["abc"];
If (hc! = Null)
{
Response. Write (hc. Expires); // 0001-1-1 0:00:00
Response. End ();
}
}
</Script>
Although the cookie of asp.net has the Expires attribute, the Response. the Write output Expires property is 0001-1-1 0:00:00 (DateTime. minValue), because the browser does not send the cookie expiration time to the server, DateTime is used. minValue to fill in the Expires attribute of the cookie.
To obtain the expiration time, you must use another cookie value to record the expiration time of the corresponding cookie. As follows:
The Code is as follows:
<Script>
Var d = new Date ();
D. setHours (d. getHours () + 1); // 1 expired when I was a child
Document. cookie = 'testvalue = 123; expires = '+ d. toGMTString (); // store the cookie value
Document. cookie = 'testexp = '+ escape (d. toLocaleString () + '; expires =' + d. toGMTString (); // store the cookie expiration time. To obtain the cookie expiration time of testvalue, you can obtain the cookie testexp.
</Script>