两种方法使用js读写cookie实现一个底部广告浮层效果
下面一个案例实现了js实现一个页面浮层并且使用两种方法使用js读写cookie;来实现用户关闭广告的显示状态;
读者可以将下面代码复制到一个html文件试试效果;html的pre标签未两种js实现的方式
<!DOCTYPE HTML>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
<meta content="杨凯" name="description"/>
<meta name="author" content="http://blog.csdn.net/tianyazaiheruan"/>
<meta name="copyright" content="杨凯版权所有"/>
<title>IT_Blog_杨凯</title>
</head>
<body>
<div>
本文作者:IT_Blog_杨凯
转载请指明出处:<a href=”http://blog.csdn.net/yangkai_hudong”>http://blog.csdn.net/yangkai_hudong</a>
</div>
<br>
<div>
<pre>
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();
});
});
</pre>
</div>
<!--广告样式 -->
<style type="text/css">
body {TEXT-ALIGN: center;}
#wapDocCookie{background-color:rgba(0,0,0,0.7);background:#4b4b4b\9;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 -36px\9;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 0\9;height:12px;width:12px;display:block;position:absolute;top:5px;right:10px;}
<!--广告js -->
</style>
<script type="text/javascript" src="http://www.huimg.cn/lib/jquery-1.3.2.js"></script>
<script type="text/javascript">
$(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;
}
};
</script> <div id="wapDocCookie" style="display: none;">
<a id="bkguancha" href="http://www.baike.com/api.php?m=guancha&a=download" onclick="StatVirtualTraffic(document.referrer,window.location,'stat_hdstat_onclick_survey_wap_doc_foot_download')">点击下载</a>
<a title="关闭" id="closeAd" href="javascript:void(0)"> </a>
</div>
</body>
</html>
两种方法使用js读写cookie实现一个底部广告浮层效果的更多相关文章
- 两种方法实现js页面隔几秒后跳转,及区别
这里需要用到window的两个对象方法,setInterval()和setTimeout() 一. 区别: 1. setInterval(code,millisec) 周期性(millisec单位 ...
- ajax后台请求两种方法(js和jQuery)
(1)js的ajax var xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ...
- Ajax中解析Json的两种方法详解
eval(); //此方法不推荐 JSON.parse(); //推荐方法 一.两种方法的区别 我们先初始化一个json格式的对象: var jsonDate = '{ "name&qu ...
- 使用JavaScript获取URL中的参数(两种方法)
本文给大家分享两种方法使用js获取url中的参数,其中方法二是使用的正则表达式方法,大家可以根据需要选择比较好的方法,废话不多说了,直接看详细介绍吧. 方法一: //取url参数 var type = ...
- Ajax中解析Json的两种方法
eval(); //此方法不推荐 JSON.parse(); //推荐方法 一.两种方法的区别 我们先初始化一个json格式的对象: var jsonDate = '{ "name" ...
- css实现div两列布局——左侧宽度固定,右侧宽度自适应(两种方法)
原文:css实现div两列布局--左侧宽度固定,右侧宽度自适应(两种方法) 1.应用场景 左侧一个导航栏宽度固定,右侧内容根据用户浏览器窗口宽度进行自适应 2.思路 首先把这个问题分步解决,需要攻克以 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- MyEclipse取消验证Js的两种方法
MyEclipse取消验证Js的两种方法 作者: 字体:[增加 减小] 类型:转载 通过js写一个web工程的相关页面时感觉很卡,修改内存也不行下面有两种解决方法,大家可以尝试下 前言:有时我们通过j ...
- C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出 ...
随机推荐
- MyBatis从入门到精通(第6章):6.3 使用枚举或其他对象
6.3 使用枚举或其他对象 在 sys_role 表中存在一个字段 enabled,这个字段只有两个可选值,0 为禁用,1 为启用.但是在 SysRole 类中,我们使用的是 Integer enab ...
- 01 语言基础+高级:1-6 集合_day02【Collection、泛型】
day02[Collection.泛型] 主要内容 Collection集合 迭代器 增强for 泛型 教学目标 能够说出集合与数组的区别 说出Collection集合的常用功能 能够使用迭代器对集合 ...
- Cocoapod-终端
安装循序: Xcode->RVM->Ruby(安装过程中需要安装homebrew)->CocoaPats 参考文章: 安装地址:http://www.cnblogs.com/dagu ...
- python如何在一个for循环中遍历多个列表
推荐使用python内置函数zip,它可以将x个y维列表变成一个zip对象,将zip对象拆包可以发现它变成了y个x维元组.我们还可以将这个对象变成一个元组或列表.如下所示: 如果是两个列表的zip,我 ...
- macbook 安装laravel5.4
1.安装composer php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');&q ...
- Linux Man手册的使用示例
转载自:https://blog.csdn.net/ac_dao_di/article/details/54718710 Linux的命令非常多,很多人在学一个命令时,首先想到的是使用百度或者谷歌,或 ...
- [原]调试实战——使用windbg调试崩溃在ComFriendlyWaitMtaThreadProc
原调试debugwindbgcrash崩溃COM 前言 这是几年前在项目中遇到的一个崩溃问题,崩溃在了ComFriendlyWaitMtaThreadProc()里,没有源码.耗费了我很大精力,最终通 ...
- Spring 的 IOC 和 AOP 的理解
Spring 的 IOC 和 AOP 的理解: https://www.jianshu.com/p/bf1adc3b75e6 对Spring的核心(AOP和IOC)的理解(大白话) https://w ...
- imx6q-plus-Android6.0下uboot添加网卡驱动
1.文件:iTOP-iMX6_android6.0.1/bootable/bootloader/uboot-imx/include/configs/mx6sabre_common.h修改如下:#def ...
- 2019-2020-1 20199324《Linux内核原理与分析》第七周作业
第六章 进程的描述和进程的创建 知识点总结 进程的描述 操作系统内核实现操作系统的三大管理功能以及对应的抽象概念: 进程管理(最核心)-- 进程 内存管理 -- 虚拟内存 文件系统 -- 文件 进程是 ...