-2

I am writing JavaScript to simulate myself clicking through links on a web page. I need to check if page is fully loaded after a link is clicked, before clicking on another link.

In short, I need to do something like this:

if(page is fully loaded) {
//someInputBoxs.value='something'
//submitButton.click() //will redirect to another page}

if(page2 is fully loaded) {
//link2.click(); }

if(page3 is fully loaded) {
//link3.click(); }

The reason to check if the page is fully loaded every time, is to avoid clicking on undefined DOM elements.

To check page load status, most of online resource suggest using window.onload, but then it can only check load state for once.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
黎蕙欣
  • 49
  • 5
  • 1
    Does this answer your question? [Pure JavaScript equivalent of jQuery's $.ready() - how to call a function when the page/DOM is ready for it](https://stackoverflow.com/questions/9899372/pure-javascript-equivalent-of-jquerys-ready-how-to-call-a-function-when-t) – Peter B Feb 21 '20 at 09:14
  • **Please note:** The OP is trying to *change pages*, not just do what's in the question title. *(Edit: I've updated the title to more accurately reflect the question.)* – T.J. Crowder Feb 21 '20 at 09:14
  • If you own (control) the pages, and if they allow being loaded in an ` – Peter B Feb 21 '20 at 09:40

1 Answers1

2

You can't have script on page A that will run when page B loads. Your page A environment, including all of your script, is stopped and thrown away when page B loads.

Instead, you'll have to have code on page A that loads page B, code on page B that loads page C, etc.

(You might [or might not] be able to do something like this in a browser extension, but not in a web page/app. You can certainly do something like this with PhantomJS, Puppeteer, or other systems that run pages in a captive browser or simulated browser environment.)

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • 1
    To add to the last paragraph, you could also have something like this in PhantomJS. OP has not specified the execution environment, so we can only presume... – Amadan Feb 21 '20 at 09:30