下面一个案例使用js实现一个页面浮层效果,并且通过两种方法使用js读写cookie来实现用户关闭广告的显示状态;
读者可以将下面代码复制到一个html文件试试效果;html的pre标签未两种js实现的方式
<BR>1.第一种:使用jQuery的cookie库 <BR>例子就是现在正在使用的js,相关代码如下: <BR>$(document).ready(function () { <BR>var adCookie=$.cookie("docCookie"); <BR>//如果本地没有cookie,将词条cookie写入本地 <BR>if(adCookie!="adDocCookie"){ <BR>$("#wapDocCookie").show(); <BR>} <BR>//如果本地存在词条cookie,不显示浮层 <BR>if(adCookie=="adDocCookie"){ <BR>$("#wapDocCookie").hide(); <BR>} <BR>//关闭广告,隐藏浮层 <BR>$("#closeAd").click(function(){ <BR>$("#wapDocCookie").hide(); <BR>$.cookie("docCookie","adDocCookie",{expires:60}); <BR>}); <br><br>}); <BR>//jQuery cookie library <BR>jQuery.cookie = function(name, value, options) { <BR>if (typeof value != 'undefined') { // name and value given, set cookie <BR>options = options || {}; <BR>if (value === null) { <BR>value = ''; <BR>options.expires = -1; <BR>} <BR>var expires = ''; <BR>if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) { <BR>var date; <BR>if (typeof options.expires == 'number') { <BR>date = new Date(); <BR>date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); <BR>} else { <BR>date = options.expires; <BR>} <BR>expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE <BR>} <BR>var path = options.path ? '; path=' + (options.path) : ''; <BR>var domain = options.domain ? '; domain=' + (options.domain) : ''; <BR>var secure = options.secure ? '; secure' : ''; <BR>document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); <BR>} else { // only name given, get cookie <BR>var cookieValue = null; <BR>if (document.cookie && document.cookie != '') { <BR>var cookies = document.cookie.split(';'); <BR>for (var i = 0; i < cookies.length; i++) { <BR>var cookie = jQuery.trim(cookies[i]); <BR>// Does this cookie string begin with the name we want? <BR>if (cookie.substring(0, name.length + 1) == (name + '=')) { <BR>cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); <BR>break; <BR>} <BR>} <BR>} <BR>return cookieValue; <BR>} <BR>}; <BR>2.第二种:自己写一个js操作cookie的实例 <BR>相关js如下: <BR>$(document).ready(function() { <br><br>function writeCookie(name,value) <BR>{ <BR>var exp = new Date(); <BR>exp.setTime(exp.getTime() + 7*24*60*60*1000); <BR>document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); <BR>} <BR>//读取cookies <BR>function readCookie(name) <BR>{ <BR>var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)"); <BR>if(arr=document.cookie.match(reg)){ <BR>return unescape(arr[2]); <BR>}else { <BR>return null; <BR>} <BR>} <BR>var adCookie = readCookie("adCookie"); <br><br>if(adCookie!="adDocCookie"){ <BR>$("#wapDocCookie").show(); <BR>} <BR>//如果本地存在词条cookie,不显示浮层 <BR>if(adCookie=="adDocCookie"){ <BR>$("#wapDocCookie").hide(); <BR>} <br><br>//关闭广告,隐藏浮层 <BR>$("#closeAd").click(function(){ <BR>$("#wapDocCookie").hide(); <BR>}); <BR>}); <BR>
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号