954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Accessing a variable defined in a function, outside the function

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(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.

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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(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.

to use a variable outside a function you need to declare the variable outside a function and run that function.

this short tutorial should help.

PS. please wrap you code with the CODE tags so that it's more easier to read. thanks

qazplm114477
Junior Poster
193 posts since Apr 2010
Reputation Points: 24
Solved Threads: 37
 

Hi,

Thanks for the reply. I know about variable scope, i just needed help getting round it.

In the end I just put the rest of my code for the page inside the same function, it required quite a lot of changes to the script but it's all I could do to work around it.

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

you're welcome, I don't think I'll be much help anymore, my knowledge in OOP in javascript is severely minimal. sorry and good luck.

qazplm114477
Junior Poster
193 posts since Apr 2010
Reputation Points: 24
Solved Threads: 37
 

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
Moderator
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
 

Ahhh of course, didnt think of that! :) I'll do that in the future. Thanks everybody for the help - it's been a while since I'v used javascript and maintained my chrome extensions :)

MaxMumford
Posting Whiz in Training
228 posts since Oct 2006
Reputation Points: 32
Solved Threads: 3
 

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
Moderator
2,683 posts since Apr 2009
Reputation Points: 321
Solved Threads: 372
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: