【发布时间】:2021-12-06 04:20:39
【问题描述】:
我想像 html 一样保存数据或值,并在再次调用页面时再次使用它们。我这里有一个来自https://www.w3schools.com/html/html5_webstorage.asp 的示例,它在 html 中是如何工作的,整个事情在 blazor 中也能工作吗?
function clickCounter() {
if (typeof(Storage) !== "undefined") {
if (localStorage.clickcount) {
localStorage.clickcount = Number(localStorage.clickcount)+1;
} else {
localStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
}
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
</body>
</html>
【问题讨论】:
-
您的意思是您不想丢失在回发中添加到您的html中的数据?
-
例如,我想存储一个用户在本地设置的变量,以便下次保存。就像我在 w3 学校写的例子一样。当页面再次被调用时,计数器会继续,尽管页面已关闭,但它已停止。
-
自然,每当在 HTML 或任何网页中发生回发时,您都会丢失客户端信息。您唯一的选择是将数据保存在后端某处并在每次回发中使用
-
嗯,好的,所以可以将数据保存在我的后端。但是我必须将它存储在我的本地存储中吗?
-
您可以使用会话、cookie 和数据库将数据保存在每次回发中,但不能保存在客户端,例如在一个简单的变量中,发布数据后您可以在任何地方保存任何内容
标签: javascript html blazor