Max,
Do exactly what qazplm said.
var whitelist;
chrome.extension.sendRequest({greeting: "whitelist"}, function(response) {
whitelist = response.whitelist;
console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"
Airshow
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
Hi all,
I'm making some changes to a google chrome extension I made and am having some trouble.
Heres my code on a content script page (removeAttr.js) :
chrome.extension.sendRequest({greeting: "whitelist"}, function whtlst(response) {
var whitelist = response.whitelist;
console.log(response.whitelist);//working
});
alert(whitelist);//alerts "undefined"
How do I acess the whitelist variable from outside the sendrequest() function?
Iv tried saving it to a window.var variable with no luck. Iv tried creating a div and assigning it's innerHTML as the whitelist variable and getting it later with no luck.
The fact that it's a chrome extension complicates things because i dont actually know if i can create elements from where the script is located.
Can anybody help me?
Max.
local variables closures or private variables are ment not to be able to access them globaly but a workaround would be to return the one you need...
chrome.extension.sendRequest({greeting: "whitelist"}, function whtlst(response) {
var whitelist = response.whitelist;
console.log(response.whitelist);//working
return whitelist});
//alert(whitelist);//alerts "undefined"
alert(whtlst())
Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
Inner functions have access to all outer members (and outer-outer, outer-outer-outer, etc. etc. right up to global members) providing their names have not been overridden by a var declaration at any stage in the scope nesting.
This is one of the basic tenets of javascript that must be grasped if we wish to avoid name conflicts and to realise the benefit of closures.
Airshow
WiFi Lounge Lizard
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372