简体   繁体   English

页面重新加载后,本地存储不保留数据

[英]localstorage not retaining data after page reload

I am trying to retain data in a localstorage after reload but it is not working this is my attempt 我试图在重新加载后将数据保留在本地存储中,但是它不起作用,这是我的尝试

<script>
window.onbeforeunload = function() {
    localStorage.setItem(name, $('#inputName').val());

}


window.onload = function() {

    var name = localStorage.getItem(name);
    if (name !== null) $('#inputName').val(name);
    alert(name);


}

</script>
<html>

<body>

 <input type="text" id="inputName" placeholder="Name" required>


</body>

</html>

on refreshing the page after entering data on the form it keeps alerting null. 在表单上输入数据后刷新页面时,它将保持警报为空。 kindly assist 请协助

In your onbeforeunload , name is the name of the window (because you haven't given it any other value, and browsers have a global name property which is the name of the window — it's usually blank). onbeforeunloadname是窗口的名称(因为您没有给它其他任何值,并且浏览器有一个全局name属性,它是窗口的名称-通常为空白)。

In this line: 在这一行:

var name = localStorage.getItem(name);

...it's undefined , because of the var name . ...由于var name undefined

You need to use a proper name, for instance: 您需要使用适当的名称,例如:

window.onbeforeunload = function() {
    localStorage.setItem("your-setting-name", $('#inputName').val());
};

window.onload = function() {
    var name = localStorage.getItem("your-setting-name");
    if (name !== null) $('#inputName').val(name);
    alert(name);
};

Also note charlietfl's oint that if you don't want to alert null on the first visit to the page, you need to put the alert in the body of the if : 还要注意charlietfl的观点,如果您不想在第一次访问该页面时向null发出警报,则需要将alert置于if的正文中:

window.onload = function() {
    var name = localStorage.getItem("your-setting-name");
    if (name !== null) {
        $('#inputName').val(name);
        alert(name);
    }
};

Otherwise, it'll alert null on the first visit and then whatever the last value was otherwise. 否则,它将在第一次访问时发出null警报,然后在其他情况下发出最后一个警报。

(Also note that I've added some missing ; . Automatic Semicolon Insertion will add these particular ones, but it's an error-correction mechanism, so I'd advise not relying on it.) (还请注意,我已经添加了一些缺少的内容; 。自动分号插入将添加这些特定的内容,但这是一种错误纠正机制,因此建议您不要依赖它。)


Other issues: 其他事宜:

  • You're using jQuery functions, but haven't shown any script tag including jQuery on the page. 您正在使用jQuery函数,但尚未在页面上显示任何脚本标签,包括jQuery。

  • You have the closing </html> tag before the <input ...> tag. <input ...>标记之前</html>结束标记。


Here's a fiddle with the above fixed (can't use Stack Snippets with local storage): https://jsfiddle.net/un86not0/ Full working page: 这是上面已修复的小提琴(不能将Stack Snippets用于本地存储): https : //jsfiddle.net/un86not0/完整的工作页面:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title>Example</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
window.onbeforeunload = function() {
    localStorage.setItem("your-setting-name", $('#inputName').val());
};

window.onload = function() {
    var name = localStorage.getItem("your-setting-name");
    if (name !== null) {
        $('#inputName').val(name);
        alert(name);
    }
};
</script>
<input type="text" id="inputName" placeholder="Name" required>
</body>
</html>

The variable name haven't been initiated yet you already used it as a value reference. 尚未启动变量name但您已经将其用作值引用。 Maybe it wasn't working well because of that? 也许是因为这样效果不好?

Here in this solution, you can separately reference the local storage item without using the variable name which might have caused it not to work. 在此解决方案中,您可以在不使用可能导致其无法使用的变量name情况下单独引用本地存储项目。

<script>
window.onbeforeunload = function() {
    localStorage.setItem('myItem', $('#inputName').val());

}


window.onload = function() {

    var name = localStorage.getItem('myItem');
    if (name !== null) $('#inputName').val(name);
    alert(name);
}

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组数据位于localStorage中,在页面重新加载后重置 - Array data in localStorage, resets after page reload 即使在页面重新加载后仍保留setTimeOut()调用 - Retaining setTimeOut() call even after page reload 如何在页面重新加载时从 localStorage 获取数据 - how to get data from localStorage on page reload Javascript在使用localStorage重新加载时不保留div id? - Javascript not retaining div id on reload with localStorage? 在页面重新加载时保留下拉菜单选择 - Retaining dropdown selection on page reload 如何在本地存储中记住附加的div并在重新加载/更改页面后显示它? - How remember in localstorage a appended div and display it after reload / change page? 能够保存游戏(使用 localStorage)并在重新加载页面后继续游戏 - the ability to save the game (using localStorage) and after reload the page continue the game 使用Angular在NgShoppingCart中重新加载页面后,将删除localStorage中的项目 - Items in localStorage are removed after page reload in NgShoppingCart using Angular 将表单序列化到 localStorage / 在页面重新加载后恢复它(没有 jQuery) - Serialize a form to localStorage / restore it after page reload (without jQuery) LocalStorage 数据 - 在 localStorage 中删除后如何将其保存在页面上? - LocalStorage data - how to save it on page, after delete them in localStorage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM