文章摘要
GPT 4
此内容根据文章生成,并经过人工审核,仅用于文章内容的解释与总结
投诉

前言

参考资料:

通过本地存储(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。