国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jquery.cookie.js用法實例詳解

2024-05-06 16:26:50
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了jquery.cookie.js用法,結合實例形式較為詳細的分析了jquery.cookie.js針對cookie的獲取、創建、設置、刪除等常用技巧,需要的朋友可以參考下
 

本文實例講述了jquery.cookie.js用法。分享給大家供大家參考,具體如下:

對cookies的操作在當訪問一個網站就無時無刻的都伴隨著我們,記錄著我們的一舉一動,并將不危害用戶隱私的信息,將以保存,這樣用戶就不用去從新再次操作重復的步驟,這樣大大方便了客戶,也增加了客戶對網站的回頭率。

jquery.cookie.js 提供了jquery中非常簡單的操作cookie的方法。

$.cookie('the_cookie'); // 獲得cookie$.cookie('the_cookie', 'the_value'); // 設置cookie$.cookie('the_cookie', 'the_value', { expires: 7 }); //設置帶時間的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 包括有效期 路徑 域名等

這個插件默認的過期是按天數計算的,我們可以修改下,按毫秒計算,修改如下:

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));}

下面舉個簡單的例子:我們需要對某個頁面進行閱讀統計,但是呢,在一段時間里(比如5分鐘),同一個人無論刷新了這個頁面多少次都好,都只能算一次。這個時候可以借助cookie來實現:

<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"> // 頁面類型,標識一組頁面 var pageType = 20110420; // 頁面id,標識唯一一個頁面 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); }) // 初始化數據,同一個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);  } } // 不插入與更新時統計訪問量 function getViewData(pageType, id){  $.ajax({   type: "get",  //使用get方法訪問后臺   dataType: "jsonp", //返回json格式的數據   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);   }  }) } // 插入或者更新頁面統計 function insert_page(pageType, id){  var j = null;  $.ajax({   type: "get",  //使用get方法訪問后臺   dataType: "jsonp", //返回json格式的數據   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程序設計有所幫助。



注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 满洲里市| 含山县| 四会市| 虎林市| 余江县| 海淀区| 南平市| 广灵县| 会同县| 乌拉特中旗| 宁化县| 萨嘎县| 故城县| 垦利县| 博罗县| 磐石市| 亳州市| 朝阳区| 南漳县| 辰溪县| 山西省| 玉溪市| 城口县| 无锡市| 沈阳市| 洛隆县| 永城市| 民和| 高要市| 库尔勒市| 德惠市| 宣威市| 图木舒克市| 永福县| 高青县| 三河市| 老河口市| 水城县| 塔河县| 攀枝花市| 靖宇县|