监听 页面回退事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>123</h1>
</body>
</html>
<script>
/**
* 1 模拟跳转页面 载入缓存
* 2 监听浏览器返回事件
* */
//定义缓存数据
let state = { 'name': '张三 - pushState' }
//新增缓存数据
window.history.pushState(state, null, '?pushState')
//打印载入的缓存
console.log('pushState')
console.log(window.history.state)
//监听浏览器返回事件
window.onpopstate = (e) => {
console.log(historyState)
}
</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
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