-
Notifications
You must be signed in to change notification settings - Fork 7
/
simple-history.js
39 lines (36 loc) · 1.07 KB
/
simple-history.js
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
/*!
* Simple History v0.5.0
*
* Copyright 2011, Jörn Zaefferer
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function(window, undefined) {
var initial = location.href;
window.SimpleHistory = {
supported: !!(window.history && window.history.pushState),
pushState: function(fragment, state) {
state = state || {};
history.pushState(state, null, fragment);
this.notify(state);
},
replaceState: function(fragment, state) {
state = state || {};
history.replaceState(state, null, fragment);
},
notify: function(state) {
this.matcher(location.pathname + location.search, state);
},
start: function(matcher) {
this.matcher = matcher;
window.addEventListener("popstate", function(event) {
// workaround to always ignore first popstate event (Chrome)
// a timeout isn't reliable enough
if (initial && initial === location.href) {
initial = null;
return;
}
SimpleHistory.notify(event.state || {});
}, false);
}
};
}(window));