Reputation: 27058
i am tying to set up a global var to be used through my entire script. and as simple as it looks i can't get it to work:
get_back = ""; //should be a global var or
var get_back = "";// another option
$.get('php/get_username.php', { username: userName }, function(data){
var get_back = data;
alert(get_back);
});
i want to be able to use the get_back
into url: 'http://www.xxx.com/users/' + get_back + ''
any ideas?
Thanks
Upvotes: 0
Views: 190
Reputation: 2567
I'm having difficulty grasping what your code is doing, but try this:
var get_back = ""; //Global Definition
$.get('php/get_username.php', { username: userName },
function(data){
get_back = data;
alert(get_back);
});
The difference is that you only use the keyboard "var" once. That way, when it is set later in the function it is the same variable. When you use var, you force a local definition.
Upvotes: 2
Reputation: 186083
Drop the var declaration from inside the callback function:
var get_back = '';
$.get('php/get_username.php', { username: userName }, function(data) {
get_back = data;
});
Upvotes: 1
Reputation: 385385
Your variable get_back
inside the callback is being declared as a local variable, hiding the outside one. Remove var
from that inner declaration, turning it into a bog-standard assignment:
var get_back = "";
$.get('php/get_username.php', { username: userName },
function(data){
get_back = data;
alert(get_back);
});
Upvotes: 1