tags:

views:

77

answers:

2

I have the following function:

var id = "10";
var type = "Type A";
var img = "myimage.jpg";

jQuery.post("my/path/somefile.php", { instance: 'getUrl', ID : id, type: type},
function(data)
{ 
    jQuery('#logo').attr("src",data.url + img);
},"json");
  1. How can I get the value of img when I'm inside the function?
  2. How can I sett img = new value from inside the function?

UPDATE

This code does NOT give a new value to the variable:

    logoUrl = "noLogo.png";

    jQuery.post("my/path/somefile.php", { instance: 'getUrl', ownerID : "123", type: "brand"},
    function(logo)
    {
        logoUrl = logo.url + "logo/";
    },"json");      

    alert(logoUrl); // This outputs noLogo.png"
+5  A: 

UPDATE

When working with callback functions, its important to pay attention to execution flow:

var img = "nice.jpg";

$.post('/path', { key: value }, function(data){
   img = "newname.jpg";
});

alert(img); // Alerts "nice.jpg"

It is because any code occurring after the callback (but not in the callback function) is executed first:

  1. Set img to nice.jpg
  2. Call $.post
  3. Call alert
  4. Set img to newname.jpg

Original answer:

If the code you are using exists exactly as you posted it, then:

  1. img is already available inside your anonymous callback function.
  2. Yes, you can change the value of img from inside the function as well.

When you declare variable with the var keyword, it is private to its current scope, but is available to any other contexts contained within its scope:

WORKS

function getPost(){
   var img = "nice.jpg";

   $.post('/path', {key:value}, function(data){
       alert(img); // alerts "nice.jpg"
   });
}

DOES NOT WORK

function changeImage(){
   var img = "nice.jpg";
   getPost();
}

function getPost(){
   $.post('/path', {key:value}, function(data){
       alert(img); // img is undefined
   });
}
Doug Neiner
ok, that works. But I'm not able to set `img = data.newValue`. Why can I only read data and not set data?
Steven
@Steven, I added more explanation to why you may be having trouble setting the variable.
Doug Neiner
I understand the sequence. But what I don't get then, is how I can retrieve the new value after jQuery.post. See my above example (same as yours more or less)
Steven
@Steven, just move your alert inside the callback function.
Doug Neiner
Yes, I know that works. The problem is that I need to use this new value further down the code - outside the callback function.
Steven
@Steven, working will callbacks is a totally different approach and it takes a while to get used to. I put together a sample to give you some ideas on how to handle it: https://gist.github.com/2173e91823b4c98c6f86 . As long as `logoUrl` is declared in a context shared by anything needing to access it, you can grab it from anywhere. However, it will only contain the new filename after you have retrieved it with the callback.
Doug Neiner
humm.. ok.... It's almost 03:00 here. So I'll get my head around this tomorrow. Thanks for your help :)
Steven
+1  A: 

You should be able to do exactly what you have there - globals are visible inside functions unless there's already a local variable of the same name declared.

Amber