Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
trigger click event for button located in previous page

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!