diff --git a/res/search.js b/res/search.js
index 4f3184cf397dfed74f66419cb8d364c97aa485a2..2e41aecda8764847faa00027879ae4671e084a47 100644
--- a/res/search.js
+++ b/res/search.js
@@ -19,9 +19,9 @@ var search_history = {
 
         var tmp = search_history.get().filter(function (x) {
             return x != query;
-        });
+        }).reverse();
         tmp.push(query);
-        localStorage.setItem('history', JSON.stringify(tmp.slice(0, tmp.max_size)));
+        localStorage.setItem('history', JSON.stringify(tmp.slice(Math.max(0, tmp.length - tmp.max_size))));
     }
 };
 
@@ -360,8 +360,12 @@ var make_toggle_context = function (buffer, id) {
 };
 
 var hashChange = function () {
-    $("#q").val(decodeURIComponent(location.hash.substr(1)));
-    search();
+    var input = $("#q");
+    var newquery = decodeURIComponent(location.hash.substr(1));
+    if (input.val() != newquery) {
+        input.val(newquery);
+        search();
+    }
 };
 
 var init = function () {