读者可以将下面代码复制到一个html文件试试效果;html的pre标签未两种js实现的方式
复制代码 代码如下:
本文作者:IT_Blog_杨凯
转载请指明出处:http://blog.csdn.net/yangkai_hudong
1.第一种:使用jQuery的cookie库
例子就是现在正在使用的js,相关代码如下:
$(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;
}
};
2.第二种:自己写一个js操作cookie的实例
相关js如下:
$(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 adcookie = readcookie("adcookie");
if(adcookie!="adDoccookie"){
$("#wapDoccookie").show();
}
//如果本地存在词条cookie,不显示浮层
if(adcookie=="adDoccookie"){
$("#wapDoccookie").hide();
}
//关闭广告,隐藏浮层
$("#closeAd").click(function(){
$("#wapDoccookie").hide();
});
});
body {TEXT-ALIGN: center;}
#wapDoccookie{background-color:rgba(0,0,0,0.7);background:#4b4b4b9;width:100%;text-align:center;position:fixed;padding:10px 0 5px 0;bottom:0;left:0;}
#bkguancha{background:url(http://static.hudong.com/35/86/26100000006141138683868789461.png) no-repeat;background-size:280px;background:url(http://static.hudong.com/50/69/26100000006141138683695381873.png) no-repeat 0 -36px9;height:46px;width:290px;display:inline-block;overflow:hidden;line-height:99em;}
#closeAd{background:url(http://static.hudong.com/54/88/26100000006141138683883031718.png) no-repeat ;background-size:12px;background:url(http://static.hudong.com/50/69/26100000006141138683695381873.png) no-repeat -278px 09;height:12px;width:12px;display:block;position:absolute;top:5px;right:10px;}
点击下载



