I have simple form on my webpage that passes search variables to an external page. The functionality is working as expected, I'd like to change how/where the results are displayed.
I'd like to display the search results on the same page as the search box, i.e I don't want a new window or tab to open. Ideally I would like to display the results in a hidden <div>
and stay away from iframes altogether, however I don't think that's possible?
Both pages (search and results) are on the same domain, however I only have access to edit the search page code.
What do I need to change in my current code in order to get this working. I'm open to suggestions if there's a better way to do this, such as using AJAX etc. Pretty new to JS so all help is much appreciated.
The results page URL is in the following format upon a successful search of the word 'sport';
http://example.com//search/C__Ssport__Orightresult__U
My code so far is as follows;
HTML
<form action="" onsubmit="return search()">
<input id="SearchInput" placeholder=" Search..." type="text">
<input type="hidden" id="Base" value="http://example.com/search/">
<input type="submit" value="Submit">
</form>
JS
<script type="text/javascript">
function search(){
var BaseURLInput, BaseURL, searchInput, searchString, locationHref, charRegExString, base64Regex;
var base64_encoding_map = {"=":"PQ==", "/": "Lw==", "\\":"XA==", "?":"Pw=="};
var escapeRegExp = function(string) {
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}
BaseURLInput = document.getElementById("Base");
searchInput = document.getElementById("SearchInput");
if (searchInput && BaseURLInput) {
BaseURL= BaseURLInput.value;
searchString = searchInput.value;
for (var specialChar in base64_encoding_map) {
charRegExString = escapeRegExp(specialChar);
base64Regex = new RegExp(charRegExString, "g");
searchString = searchString.replace(base64Regex, base64_encoding_map[specialChar])
}
searchString = encodeURIComponent(searchString);
locationHref = BaseURL+ "C__S" + searchString + "__Orightresult__U";
window.open(locationHref,'_blank' );
}
return false;
}
</script>