Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Updating a Text Variable from a .js File

    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

    P.S.

    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.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I do that now. The question/problem is how to change the file without reloading.

    Thanks.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have to apologize as I now find that I posted an almost identical post just over a year ago:

    /showthread.php?t=20670

    I was advised to use this code:

    x=document.createElement('script');
    x.src='list_mcdonalds.js';
    document.body.appendChild(x);

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •