-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.html
68 lines (60 loc) · 1.5 KB
/
index.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
<!DOCTYPE html>
<html class="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>history</title>
</head>
<body>
<button onclick="a()">a</button>
<button onclick="b()">b</button>
<button onclick="c()">b</button>
<p id="box">^!^</p>
<script>
function a() {
history.pushState({
name: 'a'
}, 'a', location.href.split('?')[0] + '?name=a');
his.eventTrigger({name: 'a'});
}
function b() {
history.pushState({
name: 'b'
}, 'b', location.href.split('?')[0] + '?name=b');
his.eventTrigger({name: 'b'});
}
function c() {
history.pushState({
name: 'c'
}, 'c', location.href.split('?')[0] + '?name=c');
his.eventTrigger({name: 'c'});
}
var His = function() {
this.eventFn = {};
window.onpopstate = (e) => {
this.eventTrigger(e.state)
};
}
His.prototype.eventTrigger = function(state) {
var name = state.name,
env = this.eventFn[ name ];
env.fn.apply(env.ctx);
};
His.prototype.bind = function(name, fn, ctx = window) {
this.eventFn[ name ] = {
fn,
ctx
}
};
var his = new His();
his.bind('a', function() {
console.log('a');
});
his.bind('b', function() {
console.log('b');
});
his.bind('c', function() {
console.log('c');
});
</script>
</body>
</html>