Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:42:21+00:00 2026-05-14T23:42:21+00:00

I’m using XMLHttpRequest, and I want to access a local variable in the success

  • 0

I’m using XMLHttpRequest, and I want to access a local variable in the success callback function.

Here is the code:

function getFileContents(filePath, callbackFn) {  
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            callbackFn(xhr.responseText);
        }
    }
    xhr.open("GET", chrome.extension.getURL(filePath), true);
    xhr.send();
}

And I want to call it like this:

var test = "lol";

getFileContents("hello.js", function(data) {
    alert(test);
});

Here, test would be out of the scope of the callback function, since only the enclosing function’s variables are accessible inside the callback function. What is the best way to pass test to the callback function so the alert(test); will display test correctly?

Edit:

Now, if I have the following code calling the function defined above:

for (var test in testers) {
    getFileContents("hello.js", function(data) {
        alert(test);
    });
}

The alert(test); code only prints the last value of test from the for loop. How do I make it so that it prints the value of test during the time at which the function getFileContents was called? (I would like to do this without changing getFileContents because it’s a very general helper function and I don’t want to make it specific by passing a specific variable like test to it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:42:22+00:00Added an answer on May 14, 2026 at 11:42 pm

    With the code you have provided test will still be in scope inside the callback. xhr will not be, other than xhr.responseText being passed in as data.

    Updated from comment:

    Assuming your code looks something like this:

    for (var test in testers)
      getFileContents("hello"+test+".js", function(data) {
        alert(test);
      });
    }
    

    As this script runs, test will be assigned the values of the keys in testers – getFileContents is called each time, which starts a request in the background. As the request finishes, it calls the callback. test is going to contain the FINAL VALUE from the loop, as that loop has already finished executing.

    There is a technique you can use called a closure that will fix this sort of problem. You can create a function that returns your callback function, creating a new scope you can hold onto your variables with:

    for (var test in testers) {
      getFileContents("hello"+test+".js", 
        (function(test) { // lets create a function who has a single argument "test"
          // inside this function test will refer to the functions argument
          return function(data) {
            // test still refers to the closure functions argument
            alert(test);
          };
        })(test) // immediately call the closure with the current value of test
      );
    }
    

    This will basically create a new scope (along with our new function) that will “hold on” to the value of test.

    Another way of writing the same sort of thing:

    for (var test in testers) {
      (function(test) { // lets create a function who has a single argument "test"
        // inside this function test will refer to the functions argument
        // not the var test from the loop above
        getFileContents("hello"+test+".js", function(data) {
            // test still refers to the closure functions argument
            alert(test);
        });
      })(test); // immediately call the closure with the value of `test` from `testers`
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.