1

I have this code in .js file:

$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {     
  if (data.address.country='spain') {
    var a="http://www.link1.com";
  } else { 
    var a="http://www.link2.com";
  }
  return a; 
});

var fan_page_url = data();

How can I store var a in var fan_page_url ?

Thank you very much.

3 Answers 3

2

Try getting rid of a and assigning the links directly.

var fan_page_url;
$.getJSON('http://api.wipmania.com/jsonp?callback=?', function(data) {
    if (data.address.country = 'spain') {
        fan_page_url = "http://www.link1.com";
    } else {
        fan_page_url = "http://www.link2.com";
    }
});
Sign up to request clarification or add additional context in comments.

5 Comments

I need to store fan_page_url value outside JSON function. I have another .js that get the value from fan_page_url from 1º .js with href=" + encodeURIComponent(fan_page_url) + "
That's fine. In the above example, fan_page_url is available outside the JSON call. To ensure that it's global, you could bind it to the window object like window.fan_page_url.
My first update was: conf.js file with, var fan_page_url = "example.com"; and an start.js file that need the value form fan_page_url located in conf.js, works OK. Now if i try your update start.js get "undefined" result with href=" + encodeURIComponent(fan_page_url) + " from conf.js
Not sure I understand your case. However, remember that $.getJSON is asynchronous and that fan_page_url only gets defined when the JSON call is done. Other JS code may be executed before $.getJSON is finished.
Now I added your code in directly in start.js before href=" + encodeURIComponent(fan_page_url) + " and still not working
0

You have two options: assigning the external variable directly, as depot suggested, or treating the $.getJSON return value as a Deferred:

$.when($.getJSON(...)).done(function(returnValue) {
    fan_page_url = returnValue;
});

The latter is useful in case you don't want to hardcode the variable you'll store the results (though in general it's not a problem, and the former option is easier/cleaner).

1 Comment

I think, I need to store fan_page_url value outside JSON function. I have another .js that get the value from fan_page_url from 1º .js with href=" + encodeURIComponent(fan_page_url) + ", it says "undefined"
0

It's an old question on which I just stumbled (looking for something slightly different) but as answers did not seem to hit the nail on the head, I thought I'd add 2 cents: if you (op) had success with a variable that was hardcoded and set manually in config.js, that you were able to grab from start.js, why not simply:

  • keep the variable declared like you did in the global scope
  • assign it a default or null or empty value:

    var fan_page_url = null; // or
    var fan_page_url = ''; // or
    var fan_page_url = 'http://url_default'; // etc...
    
  • then update the global variable inside the json function:

    $.getJSON('http://api.wipmania.com/jsonp?callback=?', function(data) {
        if (data.address.country='spain') {
            fan_page_url = "http://url1";
        } else { 
            fan_page_url = "http://url2";
        }
    });
    
  • in your start.js page, you can always perform a check to see if the variable is set or not, or still carries it's default value or not and act accordingly...

It is likely that if it used to work with your normally, manually declared variable, it would work the same here as you would have changed nothing structurally, only would be updating the variable after the json response.

Answer posted for the posterity, it may help someone in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.