From: Vladimír Vondruš Date: Tue, 1 Jan 2019 23:59:18 +0000 (+0100) Subject: doxygen: hide the search by going back only when it makes sense. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=44ce1fae16778d95c5043c0cdbea7670491e9344;p=blog.git doxygen: hide the search by going back only when it makes sense. If we landed directly on an URL with #search, going back would mean the browser is redirected back to the referrer page, the Speed Dial or whatever. Do not want. In this case we simply replace the history state without the #search hash -- there definitely wasn't any other hash we could return to. --- diff --git a/doxygen/search.js b/doxygen/search.js index 05bda57f..330351a7 100644 --- a/doxygen/search.js +++ b/doxygen/search.js @@ -40,6 +40,10 @@ var Search = { only after mouse moves */ mouseMovedSinceLastRender: false, + /* Whether we can go back in history in order to hide the search box or + not. We can't do that if we arrived directly on #search from outside. */ + canGoBackToHideSearch: false, + init: function(buffer, maxResults) { let view = new DataView(buffer); @@ -512,6 +516,7 @@ function updateForSearchVisible() { /* istanbul ignore next */ function showSearch() { window.location.hash = '#search'; + Search.canGoBackToHideSearch = true; updateForSearchVisible(); return false; @@ -519,8 +524,17 @@ function showSearch() { /* istanbul ignore next */ function hideSearch() { - /* Go back to the previous state (that removes the #search hash) */ - window.history.back(); + /* If the search box was opened using showSearch(), we can go back in the + history. Otherwise (for example when we landed to #search from a + bookmark or another server), going back would not do the right thing and + in that case we simply replace the current history state. */ + if(Search.canGoBackToHideSearch) { + Search.canGoBackToHideSearch = false; + window.history.back(); + } else { + window.location.hash = '#!'; + window.history.replaceState('', '', window.location.pathname); + } /* Restore scrollbar, prevent page layout jumps */ document.body.style.overflow = 'auto';