Here's two scripts for an e-commerce website, it takes the input from the search bar and creates an httprequest to the AWS api (in order to show the searched item page). To make it easier I'm trying to take just the first input word.
I even tried to use onreadystatechange instead of onload.
In the HTML I included search.js before pageLoading.js so that it's in the right order.
pageLoading.js:
var searchBar = document.getElementById("searchBarH");
searchBar.addEventListener("search", function(Event) {
searchByInput();
});
search.js:
function searchByInput() {
function init() {
var request = new XMLHttpRequest();
return request;
}
function start(nameSearch) {
xmlHttp.open("GET", nameSearch, true);
xmlHttp.onreadystatechange = fun();
xmlHttp.send();
}
function fun() {
alert(xmlHttp.readyState); // it prints 1
object = JSON.parse(xmlHttp.response); //can't get the response because readyState=1
window.location.assign('file:///C:/MAMP/htdocs/10shop/product.html?id=' + object.items[3].id);
}
var textInput = document.getElementById("searchBarH").value;
var searchTokens = new Array(" ");
var object;
searchTokens = textInput.split(" ");
const itemApi = '*AWS API URL*';
var nameSearch = itemApi + "?name=" + searchTokens[0];
var xmlHttp = init();
var starttt = start(nameSearch);
}