-3
$(document).ready(function () 
{
    $.ajax(
    {
        url: "Bibliotheek.xml",
        dataType: "xml",
        success: function (data) 
        {
            var song = $(data).find('key').filter(function () 
            {
                return $(this).text().indexOf('Name') != -1;
            }).each(function() 
            {
                window['globalVar']= $(this).next('string').text();
                console.log(globalVar);
            });
        }
    });
});

I want to use globalVar outside that each loop. But once i put de console.log outside the function. It tells my globalVar is undefined.Is it also possible to use that variable later on in javascript code?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Brent
  • 43
  • 9
  • Smells like an asynchrounity issue! – Ram May 03 '14 at 15:01
  • `window.globalVar = globalVar` is irrelevant. When you declare a variable without the "var" keyword it's added to the window object. – Wilmer May 03 '14 at 15:06
  • Well, that changes everything! You didn't tell us the most important thing: that you want to put `console.log` outside of **ajax** call. No wonder it is undefined. You are trying to log a variable that was not set yet (because ajax didn't finish yet). – freakish May 03 '14 at 15:08
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Ram May 03 '14 at 15:09
  • it should work outside the each loop just not outside the ajax call... – Wilmer May 03 '14 at 15:10
  • yeah, i had to put async:false in my ajax call. But now when i log, it just shows 1 result but i need all results – Brent May 03 '14 at 15:16

2 Answers2

1

This probably happens, because you loop over an empty list (i.e. it never enters the .each callback). This thing is wrong: .find('key'). It searches for a key tag (which is not HTML, unless you actually are not dealing with HTML?). Perhaps you were looking for .find('.key')?

EDIT: It seems that you want to put console.log outside of ajax call. If you do, then you're out of luck, since you are trying to log a variable that does not exist yet. That's because a in ajax stands for asynchronous, i.e. the piece of code will run later.

EDIT 2: Welcome to asynchronous programming! It seems that you are trying to force ajax to be synchronous, which is wrong and pure eveil. Don't do it. You're code should be similar to this:

var my_fn = function(clb) {   // <-- this is callback to be called later
    var els = [];
    $.ajax({
        url: "Bibliotheek.xml",
        dataType: "xml",
        success: function (data) {
            var song = $(data).find('key').filter(function () {
                return $(this).text().indexOf('Name') != -1;
            }).each(function() {
                var el = $(this).next('string').text();
                els.push(el);
            });
            clb(els);   // <-- call it now
        }
    });
};

$(document).ready(function() {
    my_fn(function(els) {
        console.log(els);
        // do coding here
    });
});
freakish
  • 54,167
  • 9
  • 132
  • 169
  • I am getting data out of an xml document so that is why i use the.find('key') or doesn't that matter anything? – Brent May 03 '14 at 15:01
  • @Brent OK, but are you sure that the result after filtering is nonempty? When you run your code does it fire `console.log` line? – freakish May 03 '14 at 15:01
  • I have updated my code so you can see where i am getting my data from – Brent May 03 '14 at 15:08
  • Can i use els in javascript also or how do i convert it? – Brent May 03 '14 at 15:33
0

Define the globalVar outside of the functions...

var globalVar;
var song = {...
     console.log(globalVar);//will work here   
 };
console.log(globalVar);//and, will work here
Christian Phillips
  • 18,399
  • 8
  • 53
  • 82