<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    tinguo002

     

    jQuery 插件設(shè)置cookie

    jquery.cookie.js使用介紹

    2011-04-21

    對cookies的操作在當(dāng)訪問一個網(wǎng)站就無時無刻的都伴隨著我們,記錄著我們的一舉一動,并將不危害用戶隱私的信息,將以保存,這樣用戶就不用去從新再次操作重復(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è)置帶時間的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ù)計算的,我們可以修改下,按毫秒計算,修改如下:

    1if (typeof options.expires === 'number') {
    2    //var days = options.expires, t = options.expires = new Date();
    3    //t.setDate(t.getDate() + days);
    4    var seconds = options.expires, t = options.expires = new Date();
    5    t.setTime(t.getTime() + seconds);
    6    //t.setTime(t.getTime() + days);
    7    //date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
    8}

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

    01<script language="javascript" src="></script>
    02<script type="text/javascript" src="></script>
    03<script language="javascript" src="></script>
    04<script type="text/javascript">
    05    // 頁面類型,標(biāo)識一組頁面
    06    var pageType = 20110420;
    07    // 頁面id,標(biāo)識唯一一個頁面
    08    var url = window.location.href;
    09    var url_arr = url.split(".");
    10    var id = url_arr[url_arr.length - 2];
    11    //var id = 2;
    12    //var cookie = $.cookie('the_cookie'+id, true, { expires: 5/24/60/60 });
    13       
    14    $(document).ready(function(){
    15        init_count(pageType, id);
    16    })
    17       
    18    // 初始化數(shù)據(jù),同一個cookie一分鐘的訪問量都算一次
    19    function init_count(pageType, id){
    20        if($.cookie('the_cookie'+id)){
    21            //alert("cookie已存在");
    22            getViewData(pageType, id);
    23        }
    24        else
    25        {
    26            // 1分鐘過期
    27            var cookie = $.cookie('the_cookie'+id, 'Gonn', { expires: 1000 * 60 * 5 });
    28            //$.cookie('the_cookie'+id, 'Gonn');
    29            //var cookie = $.cookie('the_cookie'+id);
    30            //alert(cookie);
    31            insert_page(pageType, id);
    32           
    33        }
    34    }
    35   
    36    // 不插入與更新時統(tǒng)計訪問量
    37    function getViewData(pageType, id){
    38        $.ajax({ 
    39            type: "get",        //使用get方法訪問后臺 
    40            dataType: "jsonp"//返回json格式的數(shù)據(jù) 
    41            jsonp:"callback",
    42            url: ", //要訪問的后臺地址 
    43            data:{"opp":"view", "pageType":pageType, "id":id},
    44            async: false,
    45            success: function(data){ 
    46                //alert(data.total);
    47                $('#pc_1').html(data.total);
    48                $('#pcm_1').html(data.record);
    49           
    50        }) 
    51    }
    52   
    53    // 插入或者更新頁面統(tǒng)計
    54    function insert_page(pageType, id){
    55        var j = null;
    56        $.ajax({ 
    57            type: "get",        //使用get方法訪問后臺 
    58            dataType: "jsonp"//返回json格式的數(shù)據(jù) 
    59            jsonp:"callback",
    60            url: ", //要訪問的后臺地址 
    61            data:{"opp":"insert", "pageType":pageType, "id":id},
    62            async: false,
    63            success: function(data){ 
    64                //alert(msg.current);
    65                //alert(msg.record);
    66                j = data;
    67                //alert("111");
    68                //alert(j.total);
    69                $('#pc_1').html(data.total);
    70                $('#pcm_1').html(data.record);
    71           
    72        }) 
    73    }
    74
    75</script>

    代碼就直接原汁原味地貼上來吧,做個記錄。

    原文:http://www.nowamagic.net/jquery/jquery_JqueryCookie.php



    歡迎大家訪問我的個人網(wǎng)站 萌萌的IT人

    posted on 2013-12-08 21:46 一堣而安 閱讀(306) 評論(0)  編輯  收藏 所屬分類: js_css

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    收藏夾

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲A∨精品一区二区三区下载| 亚洲av丰满熟妇在线播放| 色在线亚洲视频www| 亚洲视频免费在线看| 亚洲最大福利视频网站| 毛片在线播放免费观看| 亚洲男人天堂av| 成人免费的性色视频| 亚洲最大成人网色香蕉| 天天摸夜夜摸成人免费视频| 亚洲午夜一区二区三区| 日本免费电影一区| 免费国产在线精品一区| 亚洲美女又黄又爽在线观看| 日本一区二区免费看| 亚洲国色天香视频| 在线a人片天堂免费观看高清| 亚洲爆乳AAA无码专区| www亚洲精品少妇裸乳一区二区| 有码人妻在线免费看片| 亚洲精品国产字幕久久不卡 | 国产一级高清视频免费看| 国产综合激情在线亚洲第一页| 亚洲第一福利网站在线观看| 中文字幕在线成人免费看| 亚洲天天在线日亚洲洲精| 我们的2018在线观看免费高清| 亚洲成AV人片在WWW| 亚洲日韩精品无码专区网址| 老汉精品免费AV在线播放| 亚洲黄页网在线观看| 亚洲一级特黄大片无码毛片| 久久久久久一品道精品免费看 | 亚洲视频免费在线看| 在线观看免费污视频| 精精国产www视频在线观看免费| 91亚洲va在线天线va天堂va国产| 成年人在线免费看视频| 久久不见久久见免费影院www日本 久久WWW免费人成—看片 | 亚洲沟沟美女亚洲沟沟| 四虎影在线永久免费观看|