/
history.js
executable file
·33 lines (30 loc) · 1.04 KB
/
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
//monkey-patching pushState
(function(history){
var pushState = history.pushState;
history.pushState = function(state) {
if (typeof history.onpushstate === "function") {
history.onpushstate({state: state});
}
return pushState.apply(history, arguments);
};
})(window.history);
//monkey-patching for replaceState
(function(history){
var replaceState = history.replaceState;
history.replaceState = function(state) {
if (typeof history.onreplacestate === "function") {
history.onreplacestate({state: state});
}
return replaceState.apply(history, arguments);
};
})(window.history);
// raise stateChange event when Push-Pop-ReplaceState captured
window.onpopstate = history.onpushstate = history.onreplacestate = function() {
var stateChange = new CustomEvent('stateChange', {detail: stateChange});
window.dispatchEvent(stateChange);
};
// raise hashChange event when onchashchange is captured
window.onhashchange = function() {
var hashChange = new CustomEvent('hashChange', {detail: hashChange});
window.dispatchEvent(hashChange);
};