5

Python、HTML、およびjavascriptを使用して作成したWebサイトがあります。メインホームページには、19の編集可能な変数フィールドがあります。これらのフィールド値のいずれかを変更してからページを離れ(他のリンクタブの1つをクリック)、ホームページに戻ると、ページがコードを再読み込みするため、すべての変数がデフォルトにリセットされます。明確にするために、「戻る」ボタンを使用すると変数は保持されますが、ほとんどの場合、ユーザーは「ホーム」リンクをクリックします。

少なくともセッションでは、どうすればWebサイトにこれらの変数を記憶させることができますか?つまり、ブラウザを閉じてWebページを再起動すると、デフォルト値になります。クッキーやAJAXは使いたくない。プロパティが変数を格納できることについて何か読んだのですが、window.nameその使い方がわかりません。変数を1つしか格納できないという点でCookieのようです。

正しく理解していれば、Cookieを使用する場合、すべての変数に対してCookieを作成する必要がありますか?それは厄介なようです。

これを行う簡単な方法はありますか?変数のリストを保存するために、Pythonで一時テキストファイルを作成する必要がありますか?それとももっと簡単なものはありますか?

編集:コードは、変数フィールドを初期化し、要素を有効/無効にするために、全体を通してdocument.getElementByIdを使用しています。

これが私が思いついた解決策です...JAndyが投稿したよりも多くの仕事。localStorage()では、変数を文字列に変換して保存し、取得するときに逆の操作を行う必要があることがわかりました。2つの関数を作成しました。1つは保存し、もう1つは変数を取得します。変数を格納するオブジェクトを作成しました。また、入力フィールドから離れるたびにローカルHTMLフィールドを更新する必要がありました。onchange = "saveTheVars()"を使用して、保存関数を呼び出しました。

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;
            }
        }
4

2 に答える 2

3

localStorageブラウザ間で広くサポートされているオブジェクト(IE8 +)を使用します。

localStorage.setItem( 'someData', 42 );

後で(Webサイトまたはブラウザーが閉じられた場合でも)

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

簡単なハウツーと制限については、MDNドキュメントをお読みください。

于 2012-07-31T23:32:03.313 に答える
0

いくつかのアイデア:

  1. 変数ごとにCookieを作成する必要はありません。すべての変数を1つの変数にパッケージ化できます。そのためにJSON形式を使用してみてください。
  2. サイトで実際のナビゲーションの代わりにアンカーナビゲーションを使用するようにします(現在のアンカーの変更にjavascriptで応答して、別のコンテンツを表示します)。ページがリロードされないため、すべての可変状態はそのままです。
于 2012-08-01T00:16:37.023 に答える