JavaScript implementation to get cookie expiration time workaround _javascript Tips

Source: Internet
Author: User

JavaScript and dynamic pages cannot get the time the cookie expires, the browser manages the expiration time, and JavaScript and dynamic pages can only set expiration time. cannot be obtained through Document.cookie (JavaScript) or Cookie.expires (asp.net) properties.

Copy Code code 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>

asp.net cookies have expires properties, but Response.Write output expires property is 0001-1-1 0:00:00 (Datetime.minvalue), this is because the browser does not send a cookie expiration time to the server, so use Datetime.minvalue to populate the cookie expires properties.

Be sure to get an expiration date, and you need to record the expiration time for the cookie by using another cookie value. As follows:

Copy Code code as follows:

<script>
var d = new Date ();
D.sethours (D.gethours () + 1); 1 Childhood expired
Document.cookie = ' testvalue=123;expires= ' + d.togmtstring (); Storing cookie values
Document.cookie = ' testexp= ' + Escape (d.tolocalestring ()) + '; expires= ' + d.togmtstring (); Store cookie expiration time, to get the expiration time of TestValue this cookie, by obtaining testexp this cookie to implement

</script>

Related Article

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.