簡體   English   中英

檢查Cookie是否存在-Chrome擴展程序

[英]Check if cookie exists - Chrome Extensions

嘿,我有一個獲取Cookie的函數,它工作正常(除了getCookies()中的if-part):

function getCookies(domain, name, callback) {
chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
    if(callback) {
        callback(cookie.value);
    } 
});
}

//USER ID
getCookies("http://free-way.me", "uid", function(id) {

    if(id == null) { document.getElementById("submulti").disabled = true;}
    else { document.getElementById("user").value = id;}
});

好吧,當沒有Cookie時,控制台會為我提供以下信息:

Error in response to cookies.get: TypeError: Cannot read property 'value' of null
at getCookies [...]

毫不奇怪,但是我不知道如何檢查請求是否有效,並返回錯誤以禁用提交按鈕。

如果您能幫助我,那就太好了。

謝謝你馬庫斯

為什么不嘗試在訪問value屬性之前對cookie的值進行快速的額外檢查?

chrome.cookies.get({'url': domain, 'name': name}, function(cookie) {
    if (callback) {
        callback(cookie ? cookie.value : null);
    }
});

三元檢查存在將確保您返回nullcookienull 讓我知道這是否不是您的問題!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM