-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashsplit.js
More file actions
68 lines (55 loc) · 2.36 KB
/
hashsplit.js
File metadata and controls
68 lines (55 loc) · 2.36 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
function hashsplit() {
function hashForm ( htmlId, hashVal ) {
if( htmlId.getAttribute('type') == 'checkbox' ) {
// check checkbox on 'true' or any string, but uncheck with 'false' and '' (empty string) only
htmlId.checked = !( hashVal === 'false' || hashVal == '' ) ;
} else {
if( typeof hashVal == "undefined") return ;
htmlId.value = hashVal ;
}
};
function hashElement (hashKey, hashVal) { // TODO this
var htmlId = document.getElementById( hashKey ) ;
if (htmlId == null ) return ;
switch( htmlId.tagName ) {
case 'INPUT' :
case 'SELECT' :
hashForm( htmlId, hashVal );
break
case 'A' :
htmlId.click();
break
}
};
(function hashrouter () {
var hashStr = decodeURIComponent(window.location.hash), hashElements
hashStr = hashStr.substring(1, hashStr.length); /* remove number sign, hash, or pound sign (#) from fragment identifier */
hashElements = hashStr.split(/[\/&]+/); /* split in array like URL query string (&) or directories (/) */
for(var i = 0; i < hashElements.length; i++) {
hashKeyVal = hashElements[i].split('='); /* in case like URL query string, split in key value */
hashElement( hashKeyVal[0] , hashKeyVal[1] );
}
})();
}
window.onhashchange = hashsplit;
function updateHash () {
var allInputs = Array.prototype.slice.call( document.getElementsByTagName("input") ) ;
var allSelect = Array.prototype.slice.call( document.getElementsByTagName("select") ) ;
var allFormfields = allInputs.concat( allSelect );
buildHash = function () {
var newhash = "#" ;
for(var i = 0; i < allFormfields.length; i++) {
if (i != 0 ) newhash += "&" ; // add leading "&" on 2 key-value, avoid "&" at the end
if( allFormfields[i].getAttribute('type') == 'checkbox' ) {
newhashvalue = allFormfields[i].checked ;
} else {
newhashvalue = allFormfields[i].value ;
}
newhash += allFormfields[i].id + "=" + newhashvalue ;
}
window.location.hash = newhash;
};
for(var i = 0; i < allFormfields.length; i++) {
allFormfields[i].onblur = function(){ buildHash() };
}
}