Make Variable Global

JavaScript related questions should go here.
Post Reply
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Make Variable Global

Post by unemployment »

How can I make my alert work? Right now it is undefined and I need to make my function in the variable global. Can this be done?
if (json_goals === undefined)
{
	ajax.get('/assets/ajax/user_goals.php', function (resp)
	{
		var json_goals = resp;
			
		return json_goals;
	});
}
alert(json_goals);
unemployment
Posts: 165
Joined: Fri May 06, 2011 5:02 pm

Re: Make Variable Global

Post by unemployment »

Fixed... no worries
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Make Variable Global

Post by jacek »

And for people who fund this page via google the solution is to declare the variable outside of a function.
var this_is_global = true;

function Im_a_function(){
    // here you can use the variable this_is_global.
}
Image
Post Reply