I wrote recently about helping users to recover from 404 errors..
I added a final line today to actually execute the search and return the results.
This is the code, if anyone’s interested. I’ve added additional comments.
// Wait for the window to load
window.onload = function populateSearchInput(){
// Only run this on 404 pages
if (document.body.classList.contains('error404')) {
// Grab the path
let searchterm = window.location.pathname;
// Strip put all the characters we don't need
searchterm = searchterm.substring(1).replaceAll("%20", " ").
replaceAll("-", " ").replaceAll("/", " ");
// Another check
// Is there a supplementary search form?
if(document.getElementById("search-form-1")) {
// Yo, let's go
// Find it
let searchbox = document.getElementById("search-form-1");
// Populate it
searchbox.value = searchterm;
// Execute it
window.location.href =
'https:\/\/design.scotentblog.co.uk\/?s=' + searchterm;
}
}
}Code language: JavaScript (javascript) 



