1

Heyho, I got a function to get cookies and it works fine (except the if-part in getCookies()):

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

Well, when there's no cookie the console gives me this:

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

No surprise, but I don't know how to check if the the request worked, and return the error to disable the submit-button.

It would be nice if you could help me..

Thank you, Markus

4

1 回答 1

2

为什么不在cookie尝试访问value属性之前对 的值进行快速的额外检查呢?

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

那里的三元检查将确保您nullcookieis时返回null。如果这不是你的问题,请告诉我!

于 2013-10-02T10:48:30.323 回答