-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhasher.js
40 lines (33 loc) · 874 Bytes
/
hasher.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
40
var Hasher = (function(w){
var loc = w.location,
hash,
hashes = {},
def,
before,
add = w.attachEvent ? 'attachEvent' : 'addEventListener',
on = w.attachEvent ? 'on' : '',
onChange = function(){
hash = loc.hash.slice(1);
var callback = hashes[hash];
before instanceof Function && before();
if(callback && callback instanceof Function)
callback();
else if(def instanceof Function){
def();
}
};
w[add](on + 'load', onChange);
w[add](on + 'hashchange', onChange);
return function(h, options){
options = options || {};
var type = typeof h,
callback;
if(type == 'object'){
for(var prop in h) (callback = h[prop]) ? (hashes[prop] = callback) : (delete hashes[prop]);
}else if(type == 'string'){
hash !== h ? (loc.hash = h) : onChange();
}
def = options.default;
before = options.beforeEach;
}
})(window);