Jquery和js实现cookie操作手机浮层广告;附加:js获取、添加、删除cookie
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>