2013-10-02 159 views
1

Heyho, 我有一个函数来获取饼干和正常工作(除如果一部分中的getCookies()):检查 - Chrome扩展

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 [...] 

没有惊喜,但我不知道如何检查请求的工作,并返回错误禁用提交按钮。

这将是,如果你能帮助我不错..

谢谢 马库斯

回答

2

为什么不干脆扔在上cookie值快速额外的检查尝试访问value属性之前?

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

三元检查存在将确保您返回nullcookienull。让我知道如果这不是你的问题!

+0

谢谢,我会在星期五尝试它,但它看起来不错。谢谢你到目前为止 – wernersbacher

+0

谢谢。作品完美 – wernersbacher