You need to include what you want to do within the callback:

$(function(){

    var youtubeurl;

    $.getJSON('data.inc.json', function(j){
         youtubeurl = j.video_url;
         so = new SWFObject(j.video_url, "TEST", "498", "278", "9.0.28","#000000", 
"true");
         //etc...
    });
        
    //lines here don't wait for the ajax call to complete before executing.
    alert(typeof youtubeurl); //should alert undefined

});


Yes, youtubeurl will be set, but only after you get the response back from the server, which happens long after the lines after $.getJSON() get executed.



josp wrote:
that's not the problem unfortunately, cause my real script is
different.
The problem must be connected to the way $.getJSON parses the data....

On 5 Mrz., 21:11, James <james.gp....@gmail.com> wrote:
The scope of your youtubeurl variable exists only within the callback
function of your getJSON(). Set the variable outside first.

$(function(){
     var youtubeurl;

     $.getJSON('data.inc.json', function(j){
          youtubeurl = j.video_url;
     });

     so = new SWFObject(youtubeurl, "TEST", "498", "278", "9.0.28",
"#000000", "true");

});

On Mar 5, 7:54 am, josp <superbla3...@googlemail.com> wrote:

hello,
I have a file data.inc.json with the variable
{
'video_url':'http://www.youtube.com/v/idididid&hl=de&fs=1'
} which I open from my javascript by
$.getJSON(" data.inc.json", function(j){ var youtubeurl = j
["video_url"]; });
The value of the variable youtubeurl looks correct (http://www.youtube.com/v/idididid&hl=de&fs=1),
but when I want to use the url for another request, nothing happens.
If I want to load the youtube video like this
so = new SWFObject(youtubeurl, "TEST", "498", "278", "9.0.28",
"#000000", "true");
nothing happens.
If I do
var testvar = "http://www.youtube.com/v/idididid&hl=de&fs=1";;
so = new SWFObject(testvar, "TEST", "498", "278", "9.0.28", "#000000",
"true");
the video is loaded and plays.
Do I have to format the j["video_url"]? all the best
Josp

Reply via email to