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 9027195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:41:57+00:00 2026-06-16T06:41:57+00:00

I know its a long question, so allow me to explain as best as

  • 0

I know its a long question, so allow me to explain as best as I can.

I have two javascript functions that I want to run after the page loads, we will call them function1() and function2().

function1() uses AJAX to retrieve information from the database that will arrange the contents in a div from the information obtained in the database. It also returns the contents from the database once the function has finished.

function2() requires the value from the database in order to run properly, so it needs to wait until function1() returns its value before function2() runs. Unfortunately my code is not working, and without going into too much detail, below is a schematic of the code:

function function1() {
if (some_cookie_exists) {
  //run some code
} else {
  //send parameters through "POST"
  xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var a = some_value_from_database;
    // change div content
  return a;
  }
  }
//sending parameters
}
function function2(a) {
//run code that depends on value of "a"
}

window.onload = function() {
var data = function1();
function2(data);

The error I get is that var data is undefined. function1() retrieves the data successfully, and runs as I intended it to, but function2() does not execute at all, because the value is missing. Can anyone figure out why I am getting this and how I should go about solving this?

NOTE: I am really only familiar with Javascript (still novice at that), I know essentially nothing about JQuery, so if you do use this to fix the code, please explain to me why this works (it will save me trouble later)

  • 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-06-16T06:41:59+00:00Added an answer on June 16, 2026 at 6:41 am

    AJAX is asynchronous (that’s what the first A stands for). The results of the AJAX operation are not available in function1(), they’re retrieved in the onreadystatechange handler that you attach to the XMLHttpRequest object. So it’s not clear how you could be doing

    var a = some_value_from_database;
    

    in function1().

    What you need to do is call function2() from the onreadystatechange handler.

    If you post the actual implementation of function1 we may be able to provide more specific details.

    UPDATE:

    Here’s how to call function2() when the value becomes available from the AJAX call:

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var a = some_value_from_database;
            function2(a);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code (Yes I know it's quite long winded, but I
I've been using Drupal for a long time, and know that it's robust and
I know its impossible for 100% protection, but something high or that works for
For any given type i want to know its default value. In C#, there
I know its a big question but I'm a complete beginner. I've limited experience
Apologies in advance, this is a long question. (TL;DR : Does anyone have any
I feel like this question ought to have been covered, but I can't seem
I know of at least two byte-code enhancer that modify the object model at
I have following question: How can I scan a complete image to get for
that's going to be a long question so bear with me :) My Application

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.