Home > Web Front-end > JS Tutorial > body text

Javascript implementation of workarounds for obtaining cookie expiration time_javascript skills

WBOY
Release: 2016-05-16 16:39:39
Original
1717 people have browsed it

Javascript and dynamic pages cannot obtain the cookie expiration time. The expiration time is managed by the browser. JavaScript and dynamic pages can only set the expiration time, which cannot be obtained through the document.cookie (javascript) or Cookie.Expires (asp.net) attribute. arrive.

Copy code The code is as follows:

<%@page language="C#" Debug="true"%>


Although the asp.net cookie has the Expires attribute, the Expires attribute output by Response.Write is 0001-1-1 0:00:00 (DateTime.MinValue). This is because the browser does not send the expiration of the cookie. Time is given to the server, so DateTime.MinValue is used to fill the Expires property of the cookie.


You must get the expiration time, and you need to use another cookie value to record the expiration time of the corresponding cookie. As follows:

Copy code The code is as follows:
<script><br> var d = new Date();<br> d.setHours(d.getHours() 1); //Expires in 1 hour<br> Document.cookie = 'testvalue=123;expires=' d.toGMTString(); //Storage cookie value<br> document.cookie = 'testexp=' escape(d.toLocaleString()) ';expires=' d.toGMTString(); //Storage cookie expiration time. To obtain the expiration time of the testvalue cookie, obtain the testexp cookie. <br>  <br> </script>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!