首頁  >  文章  >  web前端  >  js讀寫cookie實作一個底部廣告浮層效果的兩種方法_javascript技巧

js讀寫cookie實作一個底部廣告浮層效果的兩種方法_javascript技巧

WBOY
WBOY原創
2016-05-16 17:06:181212瀏覽

下面一個案例使用js實現一個頁面浮層效果,並且透過兩種方法使用js讀寫cookie來實現使用者關閉廣告的顯示狀態;

讀者可以將下面程式碼複製到一個html檔案試試看效果;html的pre標籤未兩種js實作的方式

複製程式碼 程式碼如下:








IT_Blog_杨凯



本文作者:IT_Blog_杨凯
转载请指明出处:http://blog.csdn.net/yangkai_hudong




 <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>




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;}
#bkgcha {background:url(http://static.hudong.com/35/86/26100000006141138683868789461.png) 不重複;background-size:280px;background:url(http://static.hudong.com/50/69/ 26100000006141138683695381873.png) no-repeat 0 -36px9;height:46px;width:290px;display:inline-block;溢位:隱藏;行高:99em;}
風格>


$(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 函式庫
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // 給定名稱和值,設定cookie
選項=選項|| {};
if (value === null) {
value = '';
options.expires = -1;
}
var 過期= ' ';
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;
}
過期= ';過期='date.toUTCString(); // 使用expires屬性,IE不支援max-age
}
var path = options.path ? ';路徑='(選項.路徑):'';
var 域= options.domain ? ';域='(選項.域):'';
var secure = options.secure ? ';安全的' : '';
document.cookie = [名稱,'=',encodeURIComponent(值),過期,路徑,域,安全].join('');
} else { / / 僅給予名稱,取得cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';') ;
for (var i = 0; i var cookie = jQuery.trim(cookies[i]);
// 這個cookie 字串是否以我們想要的名稱開頭?
if (cookie.substring(0, name.length 1) == (name '=')) {
cookieValue =decodeURIComponent(cookie.substring(name.length 1));
休息;
}
}
}
返回cookieValue;
}
};
腳本>


身體>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn