`
z_piaoyi
  • 浏览: 22333 次
  • 性别: Icon_minigender_2
  • 来自: 河北
社区版块
存档分类
最新评论

对cookie的操作

阅读更多

function getCookie(name)
{ // 使用名称参数取得Cookie值, null表示Cookie不存在
  var strCookies = document.cookie;
  var cookieName = name + "=";  // Cookie名称
  var valueBegin, valueEnd, value;
  // 寻找是否有此Cookie名称
  valueBegin = strCookies.indexOf(cookieName);
  if (valueBegin == -1) return null;  // 没有此Cookie
  // 取得值的结尾位置
  valueEnd = strCookies.indexOf(";", valueBegin);
  if (valueEnd == -1)
      valueEnd = strCookies.length;  // 最後一个Cookie
  // 取得Cookie值
  value = strCookies.substring(valueBegin+cookieName.length,valueEnd);
  return value;
}//end function getCookie

function saveCookie(name, value, expires, path, domain, secure)
{                                                                             // 保存Cookie
  var strCookie = name + "=" + value;
  if (expires){                                                        // 计算Cookie的期限, 参数为天数
     var curTime = new Date();
     curTime.setTime(curTime.getTime() + expires*24*60*60*1000);
     strCookie += "; expires=" + curTime.toGMTString();
  }
  // Cookie的路径
  strCookie += "; path=/";
  // Cookie的Domain
  strCookie +=  (domain) ? "; domain=" + domain : "";
  // 是否需要保密传送,为一个布尔值
  strCookie +=  (secure) ? "; secure" : "";
  document.cookie = strCookie;
}

function checkCookieExist(name)
{                                                                             // 检查Cookie是否存在
       if (getCookie(name))
              return true;
       else
              return false;
}//end function checkCookieExist

function deleteCookie(name, path, domain)
{                                                                             // 删除Cookie
       var strCookie;
       if (checkCookieExist(name))
       {                                                                          // 设置Cookie的期限为己过期
              strCookie = name + "=";
              strCookie += (path) ? "; path=" + path : "";
              strCookie += (domain) ? "; domain=" + domain : "";
              strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT";
              document.cookie = strCookie;
       }//end if
}//end function deleteCookie

 

 

 

 

 

例:

<select onchange="saveQoocoVoice(this)"  name="voice_sel" >

                <option value="0">Easy</option>
                <option value="5">Medium</option>
               <option value="10">Hard</option>
   </select>
 
function saveQoocoVoice(obj){
     saveCookie("qoocoVoiceSetting", obj.value, 365);
}

function getVoiceSetting(){
    if (getCookie("qoocoVoiceSetting")!=null){
         var v  = getCookie("qoocoVoiceSetting");
         var r = document.getElementsByName("voice_sel")[0];
  if(v==5)
  r.selectedIndex = 1;
  else if(v==10)
    r.selectedIndex = 2;
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics