Wait for page load before continuing JavaScript execution

Viewed 4627

I'm new to JavaScript and trying to get this Tampermonkey script working. The scripts works just fine when it collects data from one page. However, I now want it to collect the data as before, but then move on to another page and continue the data collecting.

My code:

$(document).ready(function() {
    $("#getData").click(function() {
        // Part 1. Collect data and move on
        window.location.href = goToURL;
        // Part 2. Collect more data when the page has fully loaded
    });
});

I have tired to put Part 2 of the code inside:

setTimeout(function() {
}, 5000);

and

window.onload = function(){
};

But I cannot get the code to work, it either executes before the page has loaded, or seemingly not at all. What am I missing?

1 Answers
Related