惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
U
Unit 42
博客园 - 叶小钗
博客园 - 聂微东
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
有赞技术团队
有赞技术团队
aimingoo的专栏
aimingoo的专栏
D
DataBreaches.Net
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
美团技术团队
The Cloudflare Blog
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
S
Schneier on Security
C
Check Point Blog
Project Zero
Project Zero
The Hacker News
The Hacker News
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cisco Talos Blog
Cisco Talos Blog
P
Privacy International News Feed
SecWiki News
SecWiki News
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
H
Help Net Security
TaoSecurity Blog
TaoSecurity Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Last Week in AI
Last Week in AI
P
Privacy & Cybersecurity Law Blog
Forbes - Security
Forbes - Security
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
T
Troy Hunt's Blog
H
Hacker News: Front Page
Vercel News
Vercel News

Minecraft-Sep

还没有域名建站?2026年最新免费域名推荐 | Minecraft-Sep 还没有域名建站?2026年最新免费域名推荐 | Sep's Blog 搞机日记2——华为儿童手表3X优化 | Minecraft-Sep 搞机日记2——华为儿童手表3X优化 | Sep's Blog Windows实用软件推荐 | Minecraft-Sep Windows实用软件推荐 | Sep's Blog 搞机日记——华为儿童手表3X破解 | Minecraft-Sep 搞机日记——华为儿童手表3X破解 | Sep's Blog 提升效率必备Windows神器PowerToys | Minecraft-Sep 提升效率必备Windows神器PowerToys | Sep's Blog 【Hexo】如何提高静态博客访问速度?Vercel+Cloudflare+CDN让你的网站速度飞起! | Minecraft-Sep 【Hexo】如何提高静态博客访问速度?Vercel+Cloudflare+CDN让你的网站速度飞起! | Sep's Blog Cloudflare Pages+Telegraph搭建简易图床 | Minecraft-Sep Cloudflare Pages+Telegraph搭建简易图床 | Sep's Blog 【Hexo】告别缓慢的 Github Pages,来试试更好的 Vercel 吧! 【Hexo】告别缓慢的 Github Pages,来试试更好的 Vercel 吧! 什么是DDos?它会对博客造成什么威胁?DDos攻击原理与防护方法 | Minecraft-Sep 什么是DDos?它会对博客造成什么威胁?DDos攻击原理与防护方法 | Sep's Blog 202409博客月报 | 第一期 | Minecraft-Sep 202409博客月报 | 第一期 | Sep's Blog 关于博客评论更换的通知 | Minecraft-Sep 关于博客评论更换的通知 | Sep's Blog 免费小型数据库——Vercel Blob初体验 | Minecraft-Sep 免费小型数据库——Vercel Blob初体验 | Sep's Blog 如何让你的博客更丰富?hexo常见插件推荐 | Minecraft-Sep 如何让你的博客更丰富?hexo常见插件推荐 | Sep's Blog 博客一周年记 | Minecraft-Sep 博客一周年记 | Sep's Blog 免费域名怎么弄?常见免费域名指南 | Minecraft-Sep 免费域名怎么弄?常见免费域名指南 | Sep's Blog 利用Cloudflare Workers搭建镜像网站 | Minecraft-Sep 利用Cloudflare Workers搭建镜像网站 | Sep's Blog API构建①——Bing每日一图接口 | Minecraft-Sep API构建①——Bing每日一图接口 | Sep's Blog 【Hexo】Twikoo--评论系统的添加(2) | Minecraft-Sep 【Hexo】Twikoo--评论系统的添加(2) | Sep's Blog 【Hexo】 Waline--评论系统的添加(1) | Minecraft-Sep 【Hexo】 Waline--评论系统的添加(1) | Sep's Blog 【Hexo】Hexo+Github搭建个人博客 | Minecraft-Sep 【Hexo】Hexo+Github搭建个人博客 | Sep's Blog 【HTML】利用localstrange临时存储数据 | Minecraft-Sep
【HTML】利用localstrange临时存储数据 | Sep's Blog
标签 · 2024-05-14 · via Minecraft-Sep

前言

通过本地存储(Local Storage),web 应用程序能够在用户浏览器中对数据进行本地的存储。

在 HTML5 之前,应用程序数据只能存储在 cookie 中,包括每个服务器请求。本地存储则更安全,并且可在不影响网站性能的前提下将大量数据存储于本地。

与 cookie 不同,存储限制要大得多(至少5MB),并且信息不会被传输到服务器。

本地存储经由起源地(origin)(经由域和协议)。所有页面,从起源地,能够存储和访问相同的数据。

注意:localstrange里的数据不是永久保存的,它可能会在浏览器自动清理或手动删除后消失。

例子

Example:登录界面

项目说明

利用localstrange做一个登录、注册页面,可以立即注册和登录。

项目图片

如果你有服务器,将数据存储在服务器上是不二之选。但是没有服务器,我们也可以通过一下方法完成此操作。

思路

  1. 确定临时变量:登录、注册的账号密码,状态

  2. 明确如何调用。

由于存储变量的单一,重复注册会覆盖之前的数据。

过程

登录:

  1. HTML样式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
label {
font-weight: bold;
}
input[type="text"],
input[type="password"],
button {
width: 100%;
margin-bottom: 10px;
padding: 8px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>

<div class="container">
<h2>Login</h2>
<form id="loginForm">
<label for="loginUsername">Username:</label>
<input type="text" id="loginUsername" name="loginUsername" required><br>
<label for="loginPassword">Password:</label>
<input type="password" id="loginPassword" name="loginPassword" required><br><br>
<button type="button" onclick="login()">Login</button>
<button type="button" onclick="window.location.href = 'reg.html';">Register</button>
</form>
</div>
</body>
</html>
  1. 定义
1
2
3
4
5
6
7
<script>
function login() {
var username = document.getElementById("loginUsername").value;
var password = document.getElementById("loginPassword").value;
var storedUsername = localStorage.getItem("username");
var storedPassword = localStorage.getItem("password");}
</script>
  1. 调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
function login() {
var username = document.getElementById("loginUsername").value;
var password = document.getElementById("loginPassword").value;
var storedUsername = localStorage.getItem("username");
var storedPassword = localStorage.getItem("password");
if (username === storedUsername && password === storedPassword) {
alert("Login successful!");
window.location.href = 'https://minecraftsep.github.io/';
} else {
alert("Invalid username or password.");
}
}
</script>

注册:

  1. HTML样式:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
label {
font-weight: bold;
}
input[type="text"],
input[type="password"],
button {
width: 100%;
margin-bottom: 10px;
padding: 8px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>

<div class="container">
<h2>Register</h2>
<form id="registerForm">
<label for="regUsername">Username:</label>
<input type="text" id="regUsername" name="regUsername" required><br>
<label for="regPassword">Password:</label>
<input type="password" id="regPassword" name="regPassword" required><br><br>
<button type="button" onclick="register()">Register</button>
</form>
</div>
</body>
</html>

2.定义+调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script>
function register() {
var username = document.getElementById("regUsername").value;
var password = document.getElementById("regPassword").value;
if (username && password) {
localStorage.setItem("username", username);
localStorage.setItem("password", password);
alert("Registration successful!");
window.location.href = 'index.html';
} else {
alert("Please enter both username and password.");
}
}
</script>

完整代码 –>

  • 登录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
label {
font-weight: bold;
}
input[type="text"],
input[type="password"],
button {
width: 100%;
margin-bottom: 10px;
padding: 8px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>

<div class="container">
<h2>Login</h2>
<form id="loginForm">
<label for="loginUsername">Username:</label>
<input type="text" id="loginUsername" name="loginUsername" required><br>
<label for="loginPassword">Password:</label>
<input type="password" id="loginPassword" name="loginPassword" required><br><br>
<button type="button" onclick="login()">Login</button>
<button type="button" onclick="window.location.href = 'reg.html';">Register</button>
</form>
</div>

<script>
function login() {
var username = document.getElementById("loginUsername").value;
var password = document.getElementById("loginPassword").value;
var storedUsername = localStorage.getItem("username");
var storedPassword = localStorage.getItem("password");
if (username === storedUsername && password === storedPassword) {
alert("Login successful!");
window.location.href = 'https://minecraftsep.github.io/';
} else {
alert("Invalid username or password.");
}
}
</script>

</body>
</html>

  • 注册
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
width: 300px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
}
label {
font-weight: bold;
}
input[type="text"],
input[type="password"],
button {
width: 100%;
margin-bottom: 10px;
padding: 8px;
box-sizing: border-box;
}
button {
background-color: #4caf50;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>

<div class="container">
<h2>Register</h2>
<form id="registerForm">
<label for="regUsername">Username:</label>
<input type="text" id="regUsername" name="regUsername" required><br>
<label for="regPassword">Password:</label>
<input type="password" id="regPassword" name="regPassword" required><br><br>
<button type="button" onclick="register()">Register</button>
</form>
</div>

<script>
function register() {
var username = document.getElementById("regUsername").value;
var password = document.getElementById("regPassword").value;
if (username && password) {
localStorage.setItem("username", username);
localStorage.setItem("password", password);
alert("Registration successful!");
window.location.href = 'index.html';
} else {
alert("Please enter both username and password.");
}
}
</script>

</body>
</html>

Exercise: 图片中转站

项目图片 –>

源代码请见评论区

总结

localstrange可以临时存放数据,但不建议长久存放。

版权所有©Minecraft-Sep。

MCSep