2017-03-12 126 views
0

我已經使用jquery cookie插件構建了幾個函數,我遇到了使它跨越頁面的問題。jquery cookie插件/讀取寫入cookie

我有設置cookie的set.html頁面和顯示頁面的show.html。每次有人將set.html隨機鍵值對添加到cookie中。

當我登錄數據set.html看起來像cookies設置和正確存儲,但是當我去show.html只有第一個鍵/值檢索。我嘗試使用路徑,但仍然無法使用。

function savePage(ID, name){ 
    deleteAllCookies(); 
    $.cookie.json = true; 
    var idContainer = ($.cookie('the_cookie_key_3')) || []; 
    var idContainerVal = ($.cookie("the_cookie_key_val_3")) || {}; 
    console.log(typeof idContainer); 
    console.log(idContainerVal); 
    if (idContainer.indexOf(ID) === -1) { idContainer.push(ID); idContainerVal[ID] = name;} 

    $.cookie('the_cookie_key_3', idContainer, { expires: 40}); 
    $.cookie('the_cookie_key_val_3', idContainerVal, { expires: 40 }); 
    console.log(idContainerVal); 
} 

function getSavedPages(){ 
    $.cookie.json = true; 
    var idContainer = ($.cookie('the_cookie_key_val_3')) || {}; 
    console.log(idContainer); 
    return idContainer; 
} 

回答

0

我做到了不使用jQuery的cookie的工作,而不是我用結合的cookie和本地存儲。這裏是代碼

if (!window.localStorage) { 
    window.localStorage = { 
    getItem: function (sKey) { 
     if (!sKey || !this.hasOwnProperty(sKey)) { return null; } 
     return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); 
    }, 
    key: function (nKeyId) { 
     return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]); 
    }, 
    setItem: function (sKey, sValue) { 
     if(!sKey) { return; } 
     document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; 
     this.length = document.cookie.match(/\=/g).length; 
    }, 
    length: 0, 
    removeItem: function (sKey) { 
     if (!sKey || !this.hasOwnProperty(sKey)) { return; } 
     document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; 
     this.length--; 
    }, 
    hasOwnProperty: function (sKey) { 
     return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); 
    } 
    }; 
    window.localStorage.length = (document.cookie.match(/\=/g) || window.localStorage).length; 
} 


function getSavedPages() { 
    var page = window.localStorage.getItem('hashStoreKeyValues'); 
    if(page==null){ 
    return []; 
    } 
    var idContainer = JSON.parse(unescape(page)); 
    return idContainer; 
} 

function savePage(ID, name) { 

    var currentStatus = JSON.parse(unescape(window.localStorage.getItem('hashStoreKeys'))); 
    var currentStatusValues = JSON.parse(unescape(window.localStorage.getItem('hashStoreKeyValues'))); 
    if(currentStatus == null){ 
     currentStatus = []; 
    } 

    if(currentStatusValues == null){ 
     currentStatusValues = {}; 
    } 

    if (currentStatus.indexOf(ID) === -1) { 
     currentStatus.push(ID); 
     currentStatusValues[ID] = name; 
    } 
    window.localStorage.setItem("hashStoreKeys", JSON.stringify(currentStatus)); 
    window.localStorage.setItem("hashStoreKeyValues", JSON.stringify(currentStatusValues)); 
} 
0

在你的第二個功能,不通過$.cookie()任何參數。你告訴它給你the_cookie_key_val_3

另外,第4行(以及類似地第5行)應該是這樣的:

var idContainer = $.cookie('the_cookie_key_3') || ''; 

由於$.cookie(string)結果返回JSON字符串,或undefined

當然,在生產中使用代碼之前,請刪除所有console.log()語句!

0

您將Cookie設置爲過期的時間太短。因此,它將無法活到足以在其他頁面中捕獲它。

使用cookie保存數據可能會非常棘手。根據我的經驗,只有在我開始運行服務器時打開的頁面才起作用。 而不是使用cookie使用localStorage。 LocalStorage允許您設置密鑰和值。

localStorage.setItem("data", the_cookie_key_3) 

其中關鍵是數據和值是the_cookie_key_3

You can retrieve the data by using localStorage.getItem("data");