我已经有了设置有1天有效期的cookie的代码但是我想设置在1小时而不是1天后过期的cookie。我怎样才能做到这一点?
HTML
<a href="#" onClick="setCookie('see', '000', 1);">Link</a>
的Javascript
<script type="text/javascript">
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value + ";path=/";
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
if(getCookie('see')=="000" && document.getElementById('stickyAds'))
document.getElementById('stickyAds').style.display='none';
</script>
答案 0 :(得分:0)
1天是24小时,所以1小时是1/24天
<a href="#" onClick="setCookie('see', '000', 1/24);">Link</a>
这就是你想要的吗?
答案 1 :(得分:0)
function setCookie(c_name, value, exhours) {
var exdate = new Date();
var val = exdate.valueOf();
val += 3600000 * exhours; // Add exhours * 3600000 milliseconds to the date
exdate = new Date(val);
var c_value = escape(value) + ((exhours == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value + ";path=/";
}