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

  • Home
  • SEARCH
  • 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 8595159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:22:11+00:00 2026-06-12T00:22:11+00:00

I need to load in some prices from a file (the server doesn’t have

  • 0

I need to load in some prices from a file (the server doesn’t have a database set up):

PriceChicken:2.16
PriceRibs:1.84
PriceBrisket:14.00
PricePulledPork:12.00
PriceSides:5.00

The javascript block like this:

var price = {
    "PriceChicken": "1",
    "PriceRibs": "1",
    "PriceBrisket": "1",
    "PricePulledPork": "1",
    "PriceSides": "1"
};

function loadVars(){
    console.log(price["PriceChicken"]);
    $.get("price.txt", function(data){
        dataarray = data.split("\n");
        for(i = 0;i < dataarray.length; i++){
            var line = dataarray[i].split(":");
            console.log(price[line[0]]);
            price[line[0]] = line[1];
            console.log(price[line[0]]);
        }
    });
    console.log(price["PriceChicken"]);
}
loadVars();

Unfortunately the function inside of the $.get part can’t seem to see the global price variable.
So for the first item it shows: 1, undeclared, 2.16, and then 1 again when it leaves the $.get function.
Is there a better way to get the global variable to work, or a better way to pull this information in?

Edit: I was stupid and didn’t check all my vars. As my penance I’m going to do it the right way and learn to use JSON.

  • 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-12T00:22:13+00:00Added an answer on June 12, 2026 at 12:22 am

    The $.get call is asynchronous. You are treating it as a synchronous call.

    The line console.log(price["PriceChicken"]); is called before the file is processed.

    Better to use JSON format.

    prices.json

    {
        "PriceChicken" : 2.16,
        "PriceRibs" : 1.84,
        "PriceBrisket" : 14.00,
        "PricePulledPork" : 12.00,
        "PriceSides" : 5.00
    }
    

    JavaScript

    var price = {};
    
    $.getJSON("prices.json", function(data){ 
        $.extend(price,data); 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a c++ application. I need to load some config data from file.
In my script, I need to load some info from disk file and during
I have an issue where I need to load a fixed-length file. Process some
I need to load some text (shader code) from a file that is placed
I need to load some data from DB to Cache on server start up.And
I need to load some resource from my DLL (i need to load them
I need to call some jQuery .load() function from flash. i Use this: import
I need some guidance around which approach to use to load binary files from
I need to load a custom function from an extern file but without causing
I need to load (de-serialize) a pre-computed list of integers from a file in

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.