溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jquery.cookie 的簡單使用方法

發布時間:2020-06-29 07:12:04 來源:網絡 閱讀:1222 作者:許琴 欄目:web開發
<script type="text/javascript" src=" jquery_1.4.2.min.js "></script>
<script type="text/javascript" src=" jquery.cookie.js "></script>
<script>
$(document).ready(function(){
    $.cookie('the_cookie', 'the_value', { expires: 1 });//1天
})
</script>



以天為單位確實很不方便,這里處理了下,以毫秒為單位。

<script type="text/javascript" src=" jquery_1.4.2.min.js" ></script>
<script type="text/javascript" src=" jquery.cookie.js" ></script>
<script>
$(document).ready(function(){
    var expireDate = new Date();
    expireDate.setTime( expireDate.getTime() + ( 1 * 60 * 60 * 1000 ) );//以【毫秒】為單位。這里設置的是1小時
    $.cookie('the_cookie', 'the_value', { expires: expireDate });
})
</script>



jquery.cookie.js

一個輕量級的cookie 插件,可以讀取、寫入、刪除 cookie。


使用方法:

1.新添加一個會話 cookie:

$.cookie('the_cookie', 'the_value');

注:當沒有指明 cookie有效時間時,所創建的cookie有效期默認到用戶關閉瀏覽器為止,所以被稱為

“會話cookie(session cookie)”。


2.創建一個cookie并設置有效時間為 7天:

$.cookie('the_cookie', 'the_value', { expires: 7 });

注:當指明了cookie有效時間時,所創建的cookie被稱為“持久 cookie (persistent  cookie)”。


3.創建一個cookie并設置 cookie的有效路徑:

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

注:在默認情況下,只有設置 cookie的網頁才能讀取該 cookie。如果想讓一個頁面讀取另一個頁面設

置的cookie,必須設置cookie的路徑。cookie的路徑用于設置能夠讀取 cookie的頂級目錄。將這

個路徑設置為網站的根目錄,可以讓所有網頁都能互相讀取 cookie (一般不要這樣設置,防止出現沖突)。


4.讀取cookie:

$.cookie('the_cookie'); // cookie存在 => 'the_value'

$.cookie('not_existing'); // cookie不存在 => null


5.刪除cookie,通過傳遞null作為cookie的值即可(注意要加上路徑):

$.cookie('the_cookie', null, { path: '/' });


----------相關參數的解釋---------------


1).expires: 365

定義cookie的有效時間,值可以是一個數字(從創建cookie時算起,為單位)或一個Date 對象。如果省略,那么創建的cookie是會話cookie,將在用戶退出瀏覽器時被刪除。


2).path: '/'

默認情況:只有設置cookie的網頁才能讀取該cookie。

定義cookie的有效路徑。默認情況下, 該參數的值為創建 cookie 的網頁所在路徑(標準瀏覽器的行為) 。

如果你想在整個網站中訪問這個cookie需要這樣設置有效路徑:path: '/'。如果你想刪除一個定義

了有效路徑的 cookie,你需要在調用函數時包含這個路徑:$.cookie('the_cookie', null,

{ path: '/' });。 domain: 'example.com'

默認值:創建 cookie的網頁所擁有的域名。


3).secure: true

默認值:false。如果為true,cookie的傳輸需要使用安全協議(HTTPS)。


4).raw: true

默認值:false。

默認情況下,讀取和寫入 cookie 的時候自動進行編碼和解碼(使用encodeURIComponent 編碼,

decodeURIComponent 解碼)。要關閉這個功能設置 raw: true 即可。






以下網上摘?。?/p>



對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="http://www.nowamagic.net/zt/access_count/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://www.nowamagic.net/zt/access_count/js/jquery.cookie.js"></script>
<script language="javascript" src="http://www.nowamagic.net/zt/access_count/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: "http://www.nowamagic.net/zt/access_count/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: "http://www.nowamagic.net/zt/access_count/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>






向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女