js 操作


var keyTime=15; //过期时间

//设置cookie
function setCookie ( name, value )
{
 expires = new Date();
 var expiration = new Date((new Date()).getTime() + keyTime * 1000);
 document.cookie = name + "=" + escape(value) + "; expires=" + expiration.toGMTString() + "; path=/";
}

//根据name取cookie

function getCookie ( name )
{
cookie_name = name + "=";
cookie_length = document.cookie.length;
cookie_begin = 0;
while (cookie_begin < cookie_length)
{
 value_begin = cookie_begin + cookie_name.length;
 if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
 {
  var value_end = document.cookie.indexOf ( ";", value_begin);
  if (value_end == -1)
  {
   value_end = cookie_length;
  }
  return unescape(document.cookie.substring(value_begin, value_end));
 }
 cookie_begin = document.cookie.indexOf ( " ", cookie_begin) + 1;
 if (cookie_begin == 0)
 {
   break;
 }
}
return null;
}

//删除cookie
function delCookie ( name )
{
var expireNow = new Date();
document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}

//===================比较两个时间的差(秒)

function getTimeFlag(name)
{
 var beginTime=getCookie(name);
 var nowTime = new Date().getTime();
 var a = (nowTime-beginTime)/1000;
 if(beginTime==null || a>keyTime) {
   return 0;
 } else{
   return 1;
 }
 //setCookie('zw_fb_time',new Date().getTime());

你可能感兴趣的:(web开发)