在没有 cookie 的情况下保留浏览器客户端 javascript/HTML 数据

2023-12-22

我创建了一个使用 Python、HTML 和 javascript 的网站。主主页有 19 个可编辑变量字段。如果我更改任何这些字段值,然后离开页面(单击我的其他链接选项卡之一),然后返回我的主页,我的所有变量都会重置回默认值,因为页面会重新加载代码。需要明确的是,使用“后退”按钮将保留变量,但大多数时候,用户将单击“主页”链接。

我怎样才能让网站记住这些变量,至少在会话中?也就是说,当我关闭浏览器并重新启动网页时,它将具有默认值。 我不想使用 cookie 或 AJAX。我读过一些关于window.nameproperty 能够存储变量,但我不明白如何使用它,而且它似乎就像一个 cookie,因为它只能存储一个变量。

如果我理解正确的话,如果我使用cookie,我就必须为每个变量创建一个cookie,对吧?这看起来很混乱。

有什么简单的方法可以做到这一点吗?我应该使用 Python 创建一个临时文本文件来存储变量列表吗?或者有什么更容易的事情吗?

编辑:代码始终使用 document.getElementById 来初始化变量字段并启用/禁用元素。

这是我想出的解决方案...比 JAndy 发布的工作更多。结果 localStorage() 要求您将变量转换为字符串,存储它们,然后在检索它们时执行相反的操作。我创建了两个函数。一项用于保存,一项用于检索变量。我创建了一个对象来存储变量。 每次我点击输入字段之外的地方时,我还必须更新本地 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;
            }
        }

Use the localStorage https://developer.mozilla.org/en/DOM/Storage对象,跨浏览器(IE8+)广泛支持。

localStorage.setItem( 'someData', 42 );

稍后(即使网站或浏览器关闭)

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

阅读 MDN 文档以获取快速操作方法和限制。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在没有 cookie 的情况下保留浏览器客户端 javascript/HTML 数据 的相关文章