I have the website example.com and on this website is a button with the id "button1" and by clicking on this button, it opens an unknown url "example.com/unknownUrl" with the known button "buttonOnUnknownPage" on it. How can i be sure that the unknown page has finished loading and i can click "buttonOnUnknownPage"?
NOTE: "example.com" is opened in another window than the script. that means, the script don't stops running after "example.com" reloads.
I have until now used this:
// open example.com and get button1
exampleWindow = window.open("http://example.com", "_blank");
button1 = exampleWindow.document.getElementById('button1');
// clicking on button 1 opens example.com/unknownUrl
button1.click();
//and now wait 1000ms until page might be loaded
setTimeout(function() {
buttonOnUnknownPage = exampleWindow.document.getElementById('buttonOnUnknownPage');
buttonOnUnknownPage.click()
}, 1000);
The problem of this is, that i need to wait everytime 1000ms and can still not be sure, that "example.com/unknownUrl" was loaded.
Is there a more efficient method, to be sure that "example.com/unknownUrl" has loaded ? Something like document.onload
?
The monitoring of some other window as it changes location is fairly complicated and the reason it's complicated is that each time the other window loads a new document, the entire
window
state is cleared and all event listeners are wiped out and a whole new document is created. So, you can't install an event listener once and keep using it because it gets wiped out each time a new link is clicked and the page location is changed.It is further complicated by the fact that the process of creating a new window for a particular URL will (in some browsers) first load a URL called
about:blank
and then load the real URL causing your monitoring to sometimes detect the loading of the about:blank internal URL, not the real URL you want to monitor. IE (even new versions) is particular bad for this (no surprise).So, it is possible to track the loading of these external windows, but it takes some hacking to get it to work. The hacking requires these steps:
.addEventListener
property. For some unknown reason, newly created windows in IE don't yet have this property. That means you can't install aload
event handler on a newly create window. Instead, you have to wait until that property is available and then you can install theload
event handler..addEventListener
property is available, see if the document is already done loading. If so, proceed with your DOM operation. If not, then register an event handler for when the document is done loading.I've created a function call
monitorWindowLoad()
for carrying out these steps above:This function can then be used to click successive links in several loading pages like this:
There's actually working demo of this concept here. This loads a file named window1a.html. The Javascript in that page opens a new window for window2.html, and when that is loaded, it clicks a specific link in that window. Clicking that link opens window3.html and when that is loaded, it clicks a link in that window which then opens window4.html. You should end up with two windows open (window1a.html and window4.html). window1a.html will contain a log of the various events it carried out.
The script in window1.html doesn't know any of the URLs. It's just clicking links and monitoring when the newly loaded window has loaded so it can click the next link and so on.