-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.html
45 lines (41 loc) · 959 Bytes
/
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>hash</title>
</head>
<body>
<a href="#a">a</a>
<a href="#b">b</a>
<a href="#c">c</a>
<script>
var Hash = function() {
this.hashFn = {};
window.onhashchange = (e) => {
var newUrl = e.newURL,
newHash = newUrl.match(/#(\w*)$/),
env = this.hashFn[ newHash[1] ];
env.fn.apply(env.ctx);
};
}
Hash.prototype.bind = function(hash, fn, ctx = window) {
this.hashFn[ hash ] = {
fn,
ctx
}
}
var router = new Hash();
router.bind('a', function() {
console.log('aaa');
});
router.bind('b', function() {
console.log('bbb');
});
router.bind('c', function() {
console.log('ccc');
});
</script>
</body>
</html>