兩種方法使用js讀寫cookie實現一個底部廣告浮層效果

y_keven發表於2013-12-27

       下面一個案例實現了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>


相關文章