-
Notifications
You must be signed in to change notification settings - Fork 28
/
hash.js
56 lines (50 loc) · 1.47 KB
/
hash.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
( function(window, undefined) {
"use strict";
var hash = (function() {
var fromHash = function() {
var params = window.location.hash ? window.location.hash.substr(1).split("&") : [],
paramsObject = {};
for(var i = 0; i < params.length; i++) {
var a = params[i].split("=");
paramsObject[a[0]] = decodeURIComponent(a[1]);
}
return paramsObject;
};
var toHash = function(params) {
var str = [];
for(var p in params) {
str.push(p + "=" + encodeURIComponent(params[p]));
}
window.location.hash = str.join("&");
};
return {
get: function(param) {
var params = fromHash();
if (param) {
return params[param];
} else {
return params;
}
},
add: function(newParams) {
var params = fromHash();
for (var p in newParams) {
params[p] = newParams[p];
}
toHash(params);
},
remove: function(removeParams) {
removeParams = (typeof(removeParams)=='string') ? [removeParams] : removeParams;
var params = fromHash();
for (var i = 0; i < removeParams.length; i++) {
delete params[removeParams[i]];
}
toHash(params);
},
clear: function() {
toHash({});
}
};
})();
window.hash = hash;
})(window);