Jquery和js實現cookie操作手機浮層廣告;附加:js獲取、新增、刪除cookie

y_keven發表於2013-12-12

1、jquery cookie包實現手機上的浮層廣告

<span style="font-size:18px;">$(document).ready(function(){
    //百科觀察浮層廣告    
var adCookie=$.cookie("docCookie");
    //如果本地沒有cookie,將詞條cookie寫入本地
    if(adCookie!="adDocCookie"){
        $("#wapDocCookie").show();
    }
    //如果本地存在詞條cookie,不顯示浮層
    if(adCookie=="adDocCookie"){
        $("#wapDocCookie").hide();
    }
    
    //關閉廣告,隱藏浮層
    $("#closeAd").click(function(){
       $("#wapDocCookie").hide();
      $.cookie("docCookie","adDocCookie",{expires:60});
    });
    
});
//jQuery cookie library
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};</span>


2.js cookie實現手機上的浮層廣告

<span style="font-size:18px;">$(document).ready(function() {

function writeCookie(name,value)
{
 var exp = new Date();
 exp.setTime(exp.getTime() + 7*24*60*60*1000);
 document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//讀取cookies
function readCookie(name)
{
 var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
 if(arr=document.cookie.match(reg)){
  return unescape(arr[2]);
 }else {
  return null;
  }
}

   var docCookie = readCookie("docCookie");
   var docFootAds = $("#guancha_foot_ads");

   if(docCookie!="docWirteCookie"){
    alert(docCookie);
	writeCookie("docCookie","docWirteCookie");
    docFootAds.show();
   }
   if(docCookie=="docWirteCookie"){
    alert(docCookie);
    docFootAds.hide();
   }

   //關閉廣告,隱藏浮層
	$("#closeAd").click(function(){
	   $("#wapDocCookie").hide();
	});
});</span>


3..網上js操作cookie的資料

js獲取cookie

<span style="font-size:18px;">//js獲取cookie
/*獲取Cookie值*/
function getCookie(c_name)
{
if(document.cookie.length>0){
   c_start=document.cookie.indexOf(c_name + "=")
   if(c_start!=-1){ 
     c_start=c_start + c_name.length+1 
     c_end=document.cookie.indexOf(";",c_start)
     if(c_end==-1) c_end=document.cookie.length
     return unescape(document.cookie.substring(c_start,c_end))
   }
}
return ""
}</span>


 
**********************************//JS操作cookies方法! *****************************************************************
<span style="font-size:18px;">//寫cookies
function setCookie(name,value) 
{ 
 var Days = 30; 
 var exp = new Date(); 
 exp.setTime(exp.getTime() + Days*24*60*60*1000); 
 document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
}
//讀取cookies 
function getCookie(name) 
{ 
 var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
 
 if(arr=document.cookie.match(reg))
 
  return unescape(arr[2]); 
 else 
  return null; 
}
//刪除cookies 
function delCookie(name) 
{ 
 var exp = new Date(); 
 exp.setTime(exp.getTime() - 1); 
 var cval=getCookie(name); 
 if(cval!=null) 
  document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
} 
//使用示例 
setCookie("name","hayden"); 
alert(getCookie("name"));
//如果需要設定自定義過期時間 
//那麼把上面的setCookie 函式換成下面兩個函式就ok;


//程式程式碼 
function setCookie(name,value,time)
{ 
 var strsec = getsec(time); 
 var exp = new Date(); 
 exp.setTime(exp.getTime() + strsec*1); 
 document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 
function getsec(str)
{ 
   alert(str); 
   var str1=str.substring(1,str.length)*1; 
   var str2=str.substring(0,1); 
   if (str2=="s")
   { 
     return str1*1000; 
   }
   else if (str2=="h")
   { 
    return str1*60*60*1000; 
   }
   else if (str2=="d")
   { 
    return str1*24*60*60*1000; 
   } 
} 
//這是有設定過期時間的使用示例: 
//s20是代表20秒 
//h是指小時,如12小時則是:h12 
//d是天數,30天則:d30
setCookie("name","hayden","s20");</span>


相關文章