In my code, there are two pushStates that I need to read separately and execute different functions for. However, when the form is not submitted, the related pushState does not trigger and results in this error:
Uncaught TypeError: Cannot read property 'name' of null at window.onpopstate (poke.js:18)
. This code is included inside the event 'DOMContentLoaded'. How can I handle reading different pushStates on onpopstate?
//code
window.onpopstate = function(event){
if(event.state.name){
getInfo(event.state.get_info,cards,pgHeader,pokeInfo,pokeDiv);
}else {
showAll(cards,pgHeader,pgParagraph,pokeInfo,pokeDiv);
};
};
all_poke.onclick = ()=>{
history.pushState(null,null,"/all_pokemons")
pokeInfo.innerHTML = '';
showAll(cards,pgHeader,pgParagraph,pokeInfo,pokeDiv);
return false;
}
form.onsubmit = function (){
pgParagraph.innerHTML = '';
var name = searchBox.value;
history.pushState({name},null,`${name}/info`)
getInfo(name,cards,pgHeader,pokeInfo,pokeDiv)
return false;
}