View Full Version : Updating a Text Variable from a .js File

06-13-2004, 09:47 PM
I have a JS calculator that this forum has been helping me with for years. It operates completely client-side; why is another story.

It calculates calories and nutrition from a large number of food lists, e.g., groceries, McDonald's, and so on. I only load one list at a time because if I loaded them all when the page loaded it would take too long and require too much memory for some users' PCs.

When the user wants to use another list, they select the list they want and their selection is saved in a cookie. The page (calculator) then reloads selecting the new list. Each of the lists is a separate .js file and is selected based on the cookie.

Each time a food is calculated the page regenerates the screen image to display the results of the calculation, but the page does not reload. The user may calculate a dozen or more foods before changing lists, and all of the foods calculated remain on the screen.

When the user changes lists, I can not save all of the calculations on the screen because there is too much data for a cookie. So I just save the totals of calories, protein, etc. When the new list is loaded the previous totals are presented and the user can add to them with new calculations.

I would like to avoid having to reload the page as it causes me to lose all the calculations presently in memory and being displayed on the screen. Is there a way to bring a new .js file into memory after the page is loaded? Without reloading the page?

Each list is a text variable with it's own name, e.g., groceries, mcdonalds, and so on. If a user could load more of these lists without reloading the page we could still have the problem of memory running out. But -- though I don't like coding it this way -- what if all food lists were given the same text variable name, e.g., list.

This way each time you loaded a new list from another .js file it would step on the current list as it would have the same name. Thus there would not be a problem with running out of memory, and I would not have to reload the page to load a new list.

Thanks for any ideas, Peter


I have a kinda interesting side note. If I could do this, I would not need to save the selected list in a cookie and then reload the page... selecting the correct list based on the cookie. It took me two years posting at this forum to learn how to conditionally load a .js file based on the cookie. But it works great.

06-13-2004, 10:16 PM
Just an idea:

You can use a hidden frame to load the js file into, you would have to reference the file in order to do the calculations.


06-13-2004, 10:33 PM
I do that now. The question/problem is how to change the file without reloading.


06-14-2004, 05:01 AM
I have to apologize as I now find that I posted an almost identical post just over a year ago:


I was advised to use this code:


I posted back that I tried it and it worked fine. But I never used it because of two follow-up posts. The first said that it wouldn't work in Opera 7; the second said that as I kept loading lists I would still have to deal with memory issues. I am confused about the memory issues.

I'm not sure I understand the above code, but I'm guessing the first line says to create a variable named x from a script or JavaScript file, and the second line points to the .js file. Don't know what the third line does.

Anyway, here's my question. If I keep using this code (replacing list_mcdonalds with the name of the desired list) won't there only be one list in memory at a time? x? And each new list I load will go into the variable x replacing the old one? This would mean that there would be no memory problem as there will certainly be enough memory for one list at a time.

Thanks for your patience, Peter