How to obtain the cookie expiration time using javascript

Source: Internet
Author: User

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>

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.