I am a beginner developing a web application when history.back() is called I want to programatically click a button in the previous page. I have 2 pages page1 and page2, when history.back is called from page2 I would like to go back to page1 and click end button that is present in page1
document.addEventListener("start", () => {
console.log("session ended");
console.log("final", Content);
history.back();
document.querySelector(".end__button").click();
});
<div class='end__button'>
<button>btn</button>
</div>
query selector either cannot find button or line is not executed. How can I solve this problem? TIA
You can create a sessionStorage to keep track of when you need to click the end button that is in page 1 when you load the page.
page 1:
if(Boolean(window.sessionStorage.getItem('end'))) //click end button
window.sessionStorage.setItem('end', false); //reset
I am using Boolean() here to turn end into a boolean (so that "false" won't be truthy), since sessionStorage turns values into strings. You can see this if you tried:
window.sessionStorage.setItem('number', 0);
window.sessionStorage.setItem('boolean', true);
typeof window.sessionStorage.getItem('number'); //string
typeof window.sessionStorage.getItem('boolean'); //string
You could also try if(window.sessionStorage.getItem('end') === 'true') as well.
page 2:
document.addEventListener('start', () => {
//do stuff
window.sessionStorage.setItem('end', true);
history.back();
});
Make sure you setItem before you call history.back().