两种方法使用js读写cookie实现一个底部广告浮层效果
来源:互联网 发布:菲律宾网络博客 编辑:程序博客网 时间:2024/05/29 06:44
下面一个案例实现了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 libraryjQuery.cookie = function(name, value, options) {if (typeof value != 'undefined') { // name and value given, set cookieoptions = 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 cookievar 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();}//读取cookiesfunction 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 libraryjQuery.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>
1 0
- 两种方法使用js读写cookie实现一个底部广告浮层效果
- Jquery和js实现cookie操作手机浮层广告;附加:js获取、添加、删除cookie
- css、js两种方法实现遮罩效果
- js读写cookie的方法
- JS读写COOKIE的方法
- JS读写COOKIE的方法
- JS读写COOKIE的方法
- js实现广告漂浮效果
- 两种方法实现Java读写文件
- Mysql读写分离的两种方法对比:Spring+JPA应用层实现 vs Amoeba中间件实现
- JavaScript实现回到顶部/底部的两种方法
- js 实现拖动层效果
- 两种方法实现文字阴影效果
- JS实现网页漂浮广告效果简单
- ASP中Cookie读写的实现方法
- js 弹窗 (加入一个黑色层,效果
- Android如何使用读写cookie的方法
- Android如何使用读写cookie的方法
- Unity3d的Animation使用注意事项
- SharePoint : 使用SPQuery对象时要注意的事项
- DM9000网卡芯片驱动的预备知识:MAC/PHY
- 【discuz x3】如何将头像和帖内等附件分离到远程服务器?
- Android Phone Hard Reset
- 两种方法使用js读写cookie实现一个底部广告浮层效果
- Hadoop相关概念整理
- Unity中播放动画和回放动画
- windows 查看端口占用情况
- 对Linux内核进程上下文和中断上下文的理解
- B. Fox Dividing Cheese
- 批量建站做优化方法可取吗?
- Sencha Touch2.3.1中自定义Ext.Msg中按钮显示信息方法
- linux上安装svn一记