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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:41:00+00:00 2026-05-30T13:41:00+00:00

My question is essentially that I am trying to make a universal Ajax function

  • 0

My question is essentially that I am trying to make a universal Ajax function solely for retrieving information which will be parsed and then automatically stored as the value of whatever called it. In this specific case it is a giant dictionary word-list for a hangman game I am making. How can I escape the Array that is created by ajaxRequest.responseText.split("\n") into whatever calls it. In my case it will be ALAMI.Hangman.Wordlist

My end goal is to be able to use ALAMI.Hangman.Wordlist[i] and have it return the value of whatever string is stored at that place in the Array.

Before you freak out this isn’t all of my code the XHR request function is called through ALAMI.XHR(); as written below but I didn’t include it in this code because I felt it was unnecessary.

ALAMI.XHR.Get = function(URL){
  "use strict";
  var ajaxRequest = ALAMI.XHR();
  var ajaxResponse;

  ajaxRequest.open("GET", URL, true);
  ajaxRequest.send(null);
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState === 4){
      ajaxResponse = ajaxRequest.responseText.split("\n");
      var extensionLocation = URL.lastIndexOf('.');
      console.log(URL.substr(extensionLocation) + " file ...... " + ajaxResponse.length + " lines.");
    }
  }
  return ajaxResponse;
}

ALAMI.Hangman = ALAMI.Hangman || {};

ALAMI.Hangman.Wordlist = ALAMI.XHR.Get('fulldictionary.txt');

//I want ALAMI.Hangman.Wordlist to be equal to the Array of ajaxRequest.responseText.split("\n")

My end goal is to be able to use ALAMI.Hangman.Wordlist[i] and have it return the value of whatever string is stored at that place in the Array.

Also I’m trying to make my ajax function a universal method that I can use an infinite number of times. for example:

 ALAMI.Hangman.Wordlist1 = ALAMI.XHR.Get('fulldictionary.txt');
 ALAMI.Hangman.Wordlist2 = ALAMI.XHR.Get('dictionary2.txt');

The end goal is:

If in the global space I write console.log(ALAMI.Hangman.Wordlist[0]); it currently shows up as undefined, however, what I want is for the array to have been stored in ALAMI.Hangman.Wordlist so that when I do that it will ouput the first value of the array.

console.log(ALAMI.Hangman.Wordlist[0]); //Should output Apple

  • 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-30T13:41:01+00:00Added an answer on May 30, 2026 at 1:41 pm

    Thanks to everyone who helped me answer this question! All the answers given contained usefull relevant information to help answer the question, however, since no one response contained all the understanding I needed to resolve my problem I am posting the answer.

    The solution is as follows:

    1. I needed to use a callback function.
    2. The callback function should only execute after the XHR request had been completed.

      -This is especially important and relevant to the fact that we are using asynchronous AJAX.

    3. I needed to make sure that ALAMI.Hangman.Wordlist was set as equal to the appropriate value ajaxResponse, and not to ALAMI.XHR.Get().

    The code I came up with is as follows.

    ALAMI.XHR.Get = function(URL, callback){ //callback argument was added
      "use strict";
      var ajaxRequest = ALAMI.XHR();
      var ajaxResponse;
    
      ajaxRequest.open("GET", URL, true);
      ajaxRequest.send(null);
      ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState === 4){
          ajaxResponse = ajaxRequest.responseText.split("\n");
          var extensionLocation = URL.lastIndexOf('.');
          console.log(URL.substr(extensionLocation) + " file, " + ajaxResponse.length + " lines");
          if(callback){
            callback(ajaxResponse); //callback is called after URL is parsed into an Array
          }
        }
      }
    }
    
    ALAMI.Hangman = ALAMI.Hangman || {};
    ALAMI.Hangman.Wordlist;
    
    ALAMI.XHR.Get('fulldictionary.txt', function(aR){ //callback function is specified
      ALAMI.Hangman.Wordlist = aR;                    //assigning the Wordlist property now works!
      console.log('Wordlist Retrieved');
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to make a parser for a (very) simple language that looks
I am trying to create a form with Sencha Touch that will create a
I have essentially a survey that is shown, and people answer questions a lot
My question is essentially a simple one, though I'm looking for as in-depth an
My question is essentially the same as question 765054 on StackOverflow. I'm only asking
My question is essentially the same as the following one but the answer did
Essentially, the question is in the title. I have a ASP.NET MVC application, and
This seems like a basic question, but I haven't found any clear answers. Essentially,
So I'm trying to make the horrible leap from VB.NET to objective-C. My only
I have developed a blog application of sorts that I am trying to allow

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.