5

I have a web site I created that uses Python, HTML, and javascript. The main home page has 19 editable variable fields. If I change any of these field values, and then leave the page (click on one of my other link tabs), and then return to my home page, all my variables get reset back to the defaults because the page reloads the code. To be clear, using the "back" button would keep the variables, but most of the time, the user will be clicking on a "home" link.

How can I have the website remember these variables, at least for the session? That is, when I close the browser and relaunch the webpage, it will have the default values. I don't want to use cookies or AJAX. I read something about the window.name property being able to store variables, but I don't understand how to use that, and it seems to be like a cookie in that it only can store one variable.

If I understand it correctly, if I use cookies, I would have to have a cookie created for every variable right? That seems messy.

Is there any easy way to do this? Should I create a temp text file with the Python to store a list of the variables? Or is there something easier?

Edit: the code is using document.getElementById all throughout to init variable fields and enable/disable elements.

Here is the solution I came up with... More work than JAndy posted. Turns out the localStorage() requires you to convert the variables to strings, to store them, and then do the opposite when you retrieve them. I created two functions. One to save and one to retrieve the variables. I created an Object to store the variables in. I had to also update my local HTML fields each time I clicked away from the input field. I used onchange="saveTheVars()" and called my save function.

varObjects = {Step:'step', Dwell:'dwell', Min:'min_att', Max:'max_att', Hold:'hold',  Roam:'roam', Dur:'duration', Clients:'n_client', TX:'tx' };

result = new Object(); // variable object to hold the retrieved data

function saveTheVars() {
            //fill up the object with the variables
            varObjects.Step = document.getElementById('step').value;
            varObjects.Dwell = document.getElementById('dwell').value;
            varObjects.Min = document.getElementById('min_att').value;
            varObjects.Max = document.getElementById('max_att').value;
            varObjects.Hold = document.getElementById('hold').value;
            varObjects.Dur = document.getElementById('duration').value;
            varObjects.Roam = document.getElementById('roamID').value;
            varObjects.Clients = document.getElementById('n_client').value;
            varObjects.TX = document.getElementById('tx').value;

            try{

                localStorage.setItem("theVars", JSON.stringify(varObjects)); // if localstorage id defined then save variables to it.

            } catch(e) {

                return false;
                }

}

function retrieveTheVars() {

             try {
                    result = JSON.parse(localStorage.getItem("theVars"));

                if(result == null) // no object exist in memory so set defaults
                {
                    alert("Test variables not saved: Loading defaults"); 
                    document.getElementById('duration').value= '300';
                    document.getElementById('n_client').value= '0';
                    document.getElementById('manual').value= "";
                    document.getElementById('step').value= '1';
                    document.getElementById('dwell').value= '0.45';
                    document.getElementById('min_att').value= '0';
                    document.getElementById('max_att').value= '30';
                    document.getElementById('hold').value= '3';
                    document.getElementById('roamID').value= '10';
                    document.getElementById('tx').value= '15';

                    saveTheVars(); //save the newly created variables
                }
                else
                {

                    //update the page variables with the retrieved data

                    document.getElementById('dwell').value= result.Dwell;
                    document.getElementById('step').value= result.Step;
                    document.getElementById('min_att').value= result.Min;
                    document.getElementById('max_att').value= result.Max;
                    document.getElementById('hold').value= result.Hold;
                    document.getElementById('roamID').value= result.Roam;
                    document.getElementById('duration').value= result.Dur;
                    document.getElementById('n_client').value= result.Clients;
                    document.getElementById('tx').value= result.TX;
                }

            } catch(e) {

                return false;
            }
        }
DavidScott612
  • 215
  • 1
  • 9
  • You could try using [PHP sessions](http://php.net/manual/en/book.session.php), they are like cookies but are stored on the server. – uınbɐɥs Jul 31 '12 at 23:43
  • did I misspell help??? crazy, Its been a long frustrating day. Thanks for correcting it. :) – DavidScott612 Aug 01 '12 at 00:03
  • one last thing.... there is probably a cleaner way to do what I did but with less code, but I did not know how. If someone who knows wants to provide this... great! – DavidScott612 Aug 03 '12 at 16:27

2 Answers2

3

Use the localStorage object, which is widely supported across browsers (IE8+).

localStorage.setItem( 'someData', 42 );

later (even when the website or browser was closed)

localStorage.getItem( 'someData' ) // === 42

Read the MDN documentation for a quick howto and restrictions.

jAndy
  • 231,737
  • 57
  • 305
  • 359
  • is LocalStorage a javascript method or a Python? and what happens when I lose focus of the page. Meaning I leave the page and then click back to it. This localStorage is not dinamic is it? Meaning don't I have to call it to update the variables before I leave the page each time? – DavidScott612 Aug 01 '12 at 00:16
  • @DavidScott612 - It is a JavaScript method. I don't think you have to call a function to update the variables, just use `setItem` at the time. – uınbɐɥs Aug 01 '12 at 00:30
  • If you want local storage to reflect the value of a variable, you will need to update local storage each time you update the variable. – Michael Mior Aug 01 '12 at 01:28
  • have any of you actually used localstorage method? Are you sure it will be persistant without me having to setitem before I leave the page? so what you are saying is I have to have a function that is like a Onlosefocus that calls the setItem property for all my 19 variables. – DavidScott612 Aug 01 '12 at 01:59
  • @DavidScott612: it should be just enough to listen for the `onbeforeunload` window event. Within the handler, you write down anything you like to localStorage. That storage is persistent, unless a user tell his browser to flash it explicitly. – jAndy Aug 01 '12 at 02:06
  • I'll give this method a try and get back to you on if it works for me. Thanks! – DavidScott612 Aug 01 '12 at 17:16
  • OK guys.... I don't think this will work, because localstorage does not save a file. its only in memory. perhaps I did not explain clearly. upon first launch of webpage, it loads default variables. If i leave the page and come back, I want it to remember the changes I did previously and fill in the fields. but as it is written now, every time the page loads it reloads the default values! I can't set a "first time" flag because it would always default to the init value. Seems the only way around this is upon loading the site I read a file, or a cookie. Help me out here! I don't see it. – DavidScott612 Aug 01 '12 at 18:07
  • `localStorage` only stores strings, so the comment `=== 42` is misleading. – Rob W Dec 05 '13 at 17:43
0

Some ideas:

  1. You don't have to create a cookie for every variable. You can package all your variables into a single variable. Try using a JSON format for that.
  2. Make your site use anchor navigation instead of real navigation (responding with javascript to changes in the current anchor to make different content show) and since the page never reloads, all your variable state is still intact.
ErikE
  • 48,881
  • 23
  • 151
  • 196
  • I don't know how to do either one of your suggestions.... sorry, Newby here. I'm not a web developer. I come from a C/C++ world. Thus the reason I posted the question here. Hoping you experts could show me the way... – DavidScott612 Aug 01 '12 at 00:26