1 jQuery(document).ready(function () {
4 function parse_url_and_load_page() {
5 // 'search', 'reverse', 'details'
6 var pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
8 if (pagename === '') pagename = 'search';
10 $('body').attr('id', pagename + '-page');
12 if (pagename === 'search' || pagename === 'reverse') {
14 } else if (pagename === 'details') {
16 } else if (pagename === 'deletable') {
17 deletable_page_load();
18 } else if (pagename === 'polygons') {
23 function is_relative_url(url) {
24 if (!url) return false;
25 if (url.match(/debug=1/)) return false;
26 if (url.indexOf('?') === 0) return true;
27 if (url.indexOf('/') === 0) return true;
28 if (url.indexOf('#') === 0) return false;
29 if (url.match(/^http/)) return false;
30 if (!url.match(/\.html/)) return true;
35 // remove any URL paramters with empty values
36 // '&empty=&filled=value' => 'filled=value'
37 function clean_up_url_parameters(url) {
38 var url_params = new URLSearchParams(url);
39 var to_delete = []; // deleting inside loop would skip iterations
40 url_params.forEach(function (value, key) {
41 if (value === '') to_delete.push(key);
43 for (var i = 0; i < to_delete.length; i += 1) {
44 url_params.delete(to_delete[i]);
46 return url_params.toString();
49 parse_url_and_load_page();
51 // load page after form submit
52 $(document).on('submit', 'form', function (e) {
55 var target_url = $(this).serialize();
56 target_url = clean_up_url_parameters(target_url);
58 window.history.pushState(myhistory, '', '?' + target_url);
60 parse_url_and_load_page();
63 // load page after click on relative URL
64 $(document).on('click', 'a', function (e) {
65 var target_url = $(this).attr('href');
66 if (!is_relative_url(target_url)) return;
71 window.history.pushState(myhistory, '', target_url);
73 parse_url_and_load_page();
76 // deal with back-button and other user action
77 window.onpopstate = function () {
78 parse_url_and_load_page();