@@ -94,6 +94,11 @@ function getThemePickerElement() {
94
94
return document . getElementById ( "theme-picker" ) ;
95
95
}
96
96
97
+ // Returns the current URL without any query parameter or hash.
98
+ function getNakedUrl ( ) {
99
+ return window . location . href . split ( "?" ) [ 0 ] . split ( "#" ) [ 0 ] ;
100
+ }
101
+
97
102
// Sets the focus on the search bar at the top of the page
98
103
function focusSearchBar ( ) {
99
104
getSearchInput ( ) . focus ( ) ;
@@ -255,7 +260,9 @@ function defocusSearchBar() {
255
260
hideSearchResults ( search ) ;
256
261
var hash = ev . newURL . slice ( ev . newURL . indexOf ( "#" ) + 1 ) ;
257
262
if ( browserSupportsHistoryApi ( ) ) {
258
- history . replaceState ( hash , "" , "?search=#" + hash ) ;
263
+ // `window.location.search`` contains all the query parameters, not just `search`.
264
+ history . replaceState ( hash , "" ,
265
+ getNakedUrl ( ) + window . location . search + "#" + hash ) ;
259
266
}
260
267
elem = document . getElementById ( hash ) ;
261
268
if ( elem ) {
@@ -1813,10 +1820,12 @@ function defocusSearchBar() {
1813
1820
// Because searching is incremental by character, only the most
1814
1821
// recent search query is added to the browser history.
1815
1822
if ( browserSupportsHistoryApi ( ) ) {
1823
+ var newURL = getNakedUrl ( ) + "?search=" + encodeURIComponent ( query . raw ) +
1824
+ window . location . hash ;
1816
1825
if ( ! history . state && ! params . search ) {
1817
- history . pushState ( query , "" , "?search=" + encodeURIComponent ( query . raw ) ) ;
1826
+ history . pushState ( query , "" , newURL ) ;
1818
1827
} else {
1819
- history . replaceState ( query , "" , "?search=" + encodeURIComponent ( query . raw ) ) ;
1828
+ history . replaceState ( query , "" , newURL ) ;
1820
1829
}
1821
1830
}
1822
1831
@@ -1926,7 +1935,7 @@ function defocusSearchBar() {
1926
1935
if ( search_input . value . length === 0 ) {
1927
1936
if ( browserSupportsHistoryApi ( ) ) {
1928
1937
history . replaceState ( "" , window . currentCrate + " - Rust" ,
1929
- window . location . href . split ( "?" ) [ 0 ] ) ;
1938
+ getNakedUrl ( ) + window . location . hash ) ;
1930
1939
}
1931
1940
hideSearchResults ( ) ;
1932
1941
} else {
@@ -2786,9 +2795,9 @@ function defocusSearchBar() {
2786
2795
if ( search_input . value !== "" && hasClass ( search , "hidden" ) ) {
2787
2796
showSearchResults ( search ) ;
2788
2797
if ( browserSupportsHistoryApi ( ) ) {
2789
- history . replaceState ( search_input . value ,
2790
- "" ,
2791
- "?search=" + encodeURIComponent ( search_input . value ) ) ;
2798
+ var extra = "?search=" + encodeURIComponent ( search_input . value ) ;
2799
+ history . replaceState ( search_input . value , "" ,
2800
+ getNakedUrl ( ) + extra + window . location . hash ) ;
2792
2801
}
2793
2802
document . title = searchTitle ;
2794
2803
}
0 commit comments