0

I use the $.get() function to process a lot of files and I need to find out the filename I am calling with from within its callback function. is there a way to do this?

while (allFilesToImport.length > 0) {
    var fileToOperate = allFilesToImport.shift();
    var jqxhr = $.get(path + '/' + fileToOperate,
        function(data, textStatus, jqXHR){ // here I need the fileToOperate variable!});
5
  • 1
    File name of what? You have always access to the XMLHTTPRequest object: api.jquery.com/jQuery.get/#jqxhr-object Commented Apr 30, 2012 at 11:11
  • what do you mean with "filename"? More info required Commented Apr 30, 2012 at 11:12
  • ahem ... we didn't you just try to use it? The anonymous function has access to the variable's scope (keyword "closure") and can therefore access the variable Commented Apr 30, 2012 at 11:24
  • 1
    because its asynchronous and there are a lot of files to be read. Commented Apr 30, 2012 at 12:21
  • again I edited my question do aks a bit more precise - please excuse my unpreciseness before... Commented Apr 30, 2012 at 12:28

3 Answers 3

2

You can access fileToOperate and mypathToTheFile inside your function - that's one of the reasons why closures are awesome.

Here's an example with a loop in case that's what you have:

var filenames = ['a', 'b', 'c'];
for(var i = 0; i < filenames.length; i++) {
    (function(filename) {
        $.get('whatever/'+filename, function(data) {
            // here you can use filename and it will point to the correct value
        });
    })(filenames[i]);
}

You could also use $.each() to iterate over the array:

$.each(filenames, function(i, filename) {
    $.get('whatever/'+filename, function(data) {
        // here you can use filename and it will point to the correct value
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

$.get() is asynchronous and I need to process a lot of files. that won't work... :-(
It will work if you use the closures properly. In case you have a loop you might need to use a self-executing function which takes your variable as an argument to avoid locking the same variable in the closure for each file. Please post some more actual code so we can properly help you!
1

Just use this variable inside callback function. It will grab variable's value from outer scope.

var mypathToTheFile = path + '/' + fileToOperate;
var jqxhr = $.get(mypathToTheFile, function(data, textStatus){ 
// here I need the mypathToTheFile variable!
     do_something(mypathToTheFile);
});

1 Comment

$.get() is asynchronous and I need to process a lot of files. that won't work... :-(
1

There a simple solution to that. JAvaScript supports closures, meaning you can use variables from outside the function scope inside the function value you pass to $.get as a callback (and everywhere else where you pass a function value)

var mypathToTheFile = path + '/' + fileToOperate;
var jqxhr = $.get(mypathToTheFile, 
                  function(data, textStatus){ 
                      // here you simply use the mypathToTheFile variable!
                 });

There are some oddities to be aware of when you use closures. Anychanges to the variable outside the function scope will be reflected inside the function. E.g

var functionValues = [],i,j;
for(i=0;i<10;i+=1){
    j = new String(i);
    functionValues[i] = function(){alert(j);};
}

for(i=0;i<10;i+=1){
    functionValues[i](); //this will alert 9 every time
}

will alert 9 10 times and not as you might expect 0,1,2,3,4,5,6,7,8,9

2 Comments

thats because I could not use this solution - but thnx!
@headkit odd since your accepted answer is using closures so you are indeed using this solution :). and that's an import reason why you should post the actual code. Had the original included a loop it would have been obvious that your problem fell into the group of oddities described above but just glad you found a solution

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.