Skip to content

Commit

Permalink
Stores search string as query parameter to create a permalink to the …
Browse files Browse the repository at this point in the history
…search (#55)

* Stores search string in querystring to create a permalink to the search

Signed-off-by: Chris Abraham <cjyabraham@gmail.com>

* fix indentation

Signed-off-by: Chris Abraham <cjyabraham@gmail.com>

---------

Signed-off-by: Chris Abraham <cjyabraham@gmail.com>
  • Loading branch information
cjyabraham authored Oct 16, 2024
1 parent 8a643ff commit f25b1b7
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion layouts/shortcodes/search_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@
<div id="search"></div>
<script>
window.addEventListener("DOMContentLoaded", (event) => {
new PagefindUI({ element: "#search", showSubResults: true });
pagefind = new PagefindUI({ element: "#search", showSubResults: true });

const urlParams = new URLSearchParams(window.location.search);
const q = urlParams.get('q');
if(q){
setTimeout(function(){
pagefind.triggerSearch(q);
}, 1000);
}

document.querySelector("#search input").addEventListener("input", function() {
var inputValue = this.value;
var queryStringVar = "q";
updateQueryString(queryStringVar, inputValue);
});
});

function updateQueryString(key, value) {
var baseUrl = window.location.href.split("?")[0];
var queryString = window.location.search.slice(1);
var urlParams = new URLSearchParams(queryString);

if (urlParams.has(key)) {
urlParams.set(key, value);
} else {
urlParams.append(key, value);
}

var newUrl = baseUrl + "?" + urlParams.toString();
// Update the browser history (optional)
history.replaceState(null, '', newUrl);
}
</script>

0 comments on commit f25b1b7

Please sign in to comment.