}
}
+ function is_relative_url(url) {
+ if (!url) return false;
+ if (url.indexOf('?') === 0) return true;
+ if (url.indexOf('/') === 0) return true;
+ if (url.indexOf('#') === 0) return false;
+ if (url.match(/^http/)) return false;
+ if (!url.match(/\.html/)) return true;
+
+ return false;
+ }
+
+ // remove any URL paramters with empty values
+ // '&empty=&filled=value' => 'filled=value'
+ function clean_up_url_parameters(url) {
+ var url_params = new URLSearchParams(url);
+ var to_delete = []; // deleting inside loop would skip iterations
+ url_params.forEach(function (value, key) {
+ if (value === '') to_delete.push(key);
+ });
+ for (var i = 0; i < to_delete.length; i += 1) {
+ url_params.delete(to_delete[i]);
+ }
+ return url_params.toString();
+ }
+
parse_url_and_load_page();
// load page after form submit
$(document).on('submit', 'form', function (e) {
e.preventDefault();
- window.history.pushState(myhistory, '', '?' + $(this).serialize());
+ var target_url = $(this).serialize();
+ target_url = clean_up_url_parameters(target_url);
+
+ window.history.pushState(myhistory, '', '?' + target_url);
parse_url_and_load_page();
});
// load page after click on relative URL
$(document).on('click', 'a', function (e) {
var target_url = $(this).attr('href');
- if (target_url && target_url.match(/^http/)) return;
- if (target_url && !target_url.match(/\.html/)) return;
+ if (!is_relative_url(target_url)) return;
e.preventDefault();
+ e.stopPropagation();
window.history.pushState(myhistory, '', target_url);