summaryrefslogtreecommitdiffstats
path: root/assets/js/hashfill.js
blob: a8317031770613e5820b01f21efce98851c3cd1e (plain)
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
(function(document, $) {
    'use strict';

    function readhash() {
	var kv = window.location.hash.substr(1).split('&');
	for(var i = 0; i < kv.length; i++) {
	    var p = kv[i].split('=');
	    var e = $(':input').filter(function(i, e) {
		return (e.dataset||{}).name === p[0];
	    });
	    if (e && e[0]) {
		e[0].value = decodeURIComponent(p[1].replace(/[+]/g, '%20'));
		if ('createEvent' in document) {
		    var ev = document.createEvent('HTMLEvents');
		    ev.initEvent('input', true, false);
		    e[0].dispatchEvent(ev);
		}
	    }
	}
    }

    if ('onhashchange' in window) {
	$(window).bind('hashchange', readhash);
    }

    document.addEventListener('readystatechange', function() {
	if (document.readyState === 'complete') {
	    readhash();
	}
    });

})(document, $);