jquery.cookie.js用法實(shí)例詳解
本文實(shí)例講述了jquery.cookie.js用法。分享給大家供大家參考,具體如下:
對cookies的操作在當(dāng)訪問一個網(wǎng)站就無時(shí)無刻的都伴隨著我們,記錄著我們的一舉一動,并將不危害用戶隱私的信息,將以保存,這樣用戶就不用去從新再次操作重復(fù)的步驟,這樣大大方便了客戶,也增加了客戶對網(wǎng)站的回頭率。
jquery.cookie.js 提供了jquery中非常簡單的操作cookie的方法。
$.cookie('the_cookie'); // 獲得cookie $.cookie('the_cookie', 'the_value'); // 設(shè)置cookie $.cookie('the_cookie', 'the_value', { expires: 7 }); //設(shè)置帶時(shí)間的cookie $.cookie('the_cookie', '', { expires: -1 }); // 刪除 $.cookie('the_cookie', null); // 刪除 cookie $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一個cookie 包括有效期 路徑 域名等
這個插件默認(rèn)的過期是按天數(shù)計(jì)算的,我們可以修改下,按毫秒計(jì)算,修改如下:
if (typeof options.expires === 'number') { //var days = options.expires, t = options.expires = new Date(); //t.setDate(t.getDate() + days); var seconds = options.expires, t = options.expires = new Date(); t.setTime(t.getTime() + seconds); //t.setTime(t.getTime() + days); //date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000)); }
下面舉個簡單的例子:我們需要對某個頁面進(jìn)行閱讀統(tǒng)計(jì),但是呢,在一段時(shí)間里(比如5分鐘),同一個人無論刷新了這個頁面多少次都好,都只能算一次。這個時(shí)候可以借助cookie來實(shí)現(xiàn):
<script language="javascript" src="/js/jquery-1.4.2.min.js"></script> <script type="text/javascript" src="/js/jquery.cookie.js"></script> <script language="javascript" src="/js/jquery.jsonp-2.1.4.min.js"></script> <script type="text/javascript"> // 頁面類型,標(biāo)識一組頁面 var pageType = 20110420; // 頁面id,標(biāo)識唯一一個頁面 var url = window.location.href; var url_arr = url.split("."); var id = url_arr[url_arr.length - 2]; //var id = 2; //var cookie = $.cookie('the_cookie'+id, true, { expires: 5/24/60/60 }); $(document).ready(function(){ init_count(pageType, id); }) // 初始化數(shù)據(jù),同一個cookie一分鐘的訪問量都算一次 function init_count(pageType, id){ if($.cookie('the_cookie'+id)){ //alert("cookie已存在"); getViewData(pageType, id); } else { // 1分鐘過期 var cookie = $.cookie('the_cookie'+id, 'Gonn', { expires: 1000 * 60 * 5 }); //$.cookie('the_cookie'+id, 'Gonn'); //var cookie = $.cookie('the_cookie'+id); //alert(cookie); insert_page(pageType, id); } } // 不插入與更新時(shí)統(tǒng)計(jì)訪問量 function getViewData(pageType, id){ $.ajax({ type: "get", //使用get方法訪問后臺 dataType: "jsonp", //返回json格式的數(shù)據(jù) jsonp:"callback", url: "/manage.php", //要訪問的后臺地址 data:{"opp":"view", "pageType":pageType, "id":id}, async: false, success: function(data){ //alert(data.total); $('#pc_1').html(data.total); $('#pcm_1').html(data.record); } }) } // 插入或者更新頁面統(tǒng)計(jì) function insert_page(pageType, id){ var j = null; $.ajax({ type: "get", //使用get方法訪問后臺 dataType: "jsonp", //返回json格式的數(shù)據(jù) jsonp:"callback", url: "/manage.php", //要訪問的后臺地址 data:{"opp":"insert", "pageType":pageType, "id":id}, async: false, success: function(data){ //alert(msg.current); //alert(msg.record); j = data; //alert("111"); //alert(j.total); $('#pc_1').html(data.total); $('#pcm_1').html(data.record); } }) } </script>
希望本文所述對大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
遠(yuǎn)離JS災(zāi)難css災(zāi)難之 js私有函數(shù)和css選擇器作為容器
當(dāng)一個項(xiàng)目龐大到一定階段,例如UI展示層采用了模塊化模板化之后,就會出現(xiàn)js災(zāi)難,css災(zāi)難,經(jīng)常出現(xiàn)以前從來不放在一起的兩個js或css莫名奇妙的被放到了一個頁面,基本的原因是模塊重用造成的2011-12-12jquery使用正則表達(dá)式驗(yàn)證email地址的方法
這篇文章主要介紹了jquery使用正則表達(dá)式驗(yàn)證email地址的方法,以實(shí)例形式分析了正則表達(dá)式的使用技巧,需要的朋友可以參考下2015-01-01jquery全選checkBox功能實(shí)現(xiàn)代碼(取消全選功能)
這篇文章主要介紹了jquery實(shí)現(xiàn)checkBox全選功能、取消全選功能,代碼簡單,大家可以直接參考使用2013-12-12jquery 實(shí)現(xiàn)回車登錄詳解及實(shí)例代碼
這篇文章主要介紹了jquery 實(shí)現(xiàn)回車登錄詳解的相關(guān)資料,整理了幾種方法及簡單實(shí)現(xiàn)實(shí)例,需要的朋友可以參考下2016-10-10js jquery獲取隨機(jī)生成id的服務(wù)器控件的三種方法
由于ASP.NET網(wǎng)頁運(yùn)行后,服務(wù)器控件會隨機(jī)生成客戶端id,jquery獲取時(shí)候不太好操作,下面為大家整理了三種方法,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07jQuery中的on與bind綁定事件區(qū)別實(shí)例詳解
bind與on的區(qū)別就在于–事件冒泡,關(guān)于jquery中的on與bind綁定事件的區(qū)別通過本文給大家實(shí)例講解,需要的朋友參考下吧2017-02-02