Skip to content

Commit 558509e

Browse files
committed
instantsearch.js: Switch params from hash (#) to search (?)
It helps the user to manipulate the browser URL. Some browser like Chrome doesn't react when the hash/anchor is modified by hand but does when the search/GET is. notice: `?q=` is intercepted by the top-right search input box so full page param has to be renamed. https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#change_a_query_parameter
1 parent 1c78eff commit 558509e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

docs/js/custom.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ $(document).ready(function() {
100100
});
101101

102102
let link = $('.ds-dropdown-menu a.search-page-link');
103-
const href = '/en/' + branchName + '/search_results/#q=' + encodeURI($('#search_input').val()) + '&p=1';
103+
const href = '/en/' + branchName + '/search_results/?sq=' + encodeURI($('#search_input').val()) + '&p=1';
104104

105105
if (!link.length) {
106106
link = $('.ds-dropdown-menu').append(`<div class="search-page-link-wrapper">

docs/js/instantsearch.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
}
2828
}
2929
let match = null;
30-
const search_query = (match = doc.location.hash.match(/q=(.*?)(&|$)/)) ? match[1] : '';
30+
const search_query = (match = doc.location.search.match(/sq=(.*?)(&|$)/)) ? match[1] : '';
3131
const parsed_search_query = decodeURI(search_query.replace('+', ' '));
32-
const search_page = (match = doc.location.hash.match(/p=(\d*?)(&|$)/)) ? match[1] : 1;
32+
const search_page = (match = doc.location.search.match(/p=(\d*?)(&|$)/)) ? match[1] : 1;
3333
const parsed_search_page = parseInt(search_page);
3434
const version = doc.location.pathname.split('/')[2];
3535
const search = instantsearch({
@@ -45,12 +45,17 @@
4545
});
4646

4747
doc.getElementById('searchbox').addEventListener('keyup', function (event) {
48-
window.location.hash = '#q=' + encodeURI(event.target.value) + '&p=1';
48+
const url = new URL(window.location);
49+
url.searchParams.set('sq', encodeURI(event.target.value));
50+
url.searchParams.set('p', 1);
51+
window.history.pushState({}, '', url);
4952
})
5053

5154
doc.getElementById('pagination').addEventListener('click', function (event) {
52-
let page = doc.getElementsByClassName('ais-Pagination-item--selected').length ? parseInt(doc.getElementsByClassName('ais-Pagination-item--selected')[0].innerText) : 1
53-
window.location.hash = window.location.hash.includes('p=') ? window.location.hash.replace(/p=\d*/, 'p=' + page) : window.location.hash + '&p=' + page;
55+
const page = doc.getElementsByClassName('ais-Pagination-item--selected').length ? parseInt(doc.getElementsByClassName('ais-Pagination-item--selected')[0].innerText) : 1
56+
const url = new URL(window.location);
57+
url.searchParams.set('p', page);
58+
window.history.pushState({}, '', url);
5459
})
5560

5661
search.addWidgets([

0 commit comments

Comments
 (0)