0

What I want to do is to click a button on a page using javascript, then wait for the next page to come up and then click another button. Earlier I was doing this:

document.getElementById("abc").click();
document.getElementById("def").click(); // I want this for page 2. 

This didn't work because page 2 didn't get loaded by that time it reached the second line of code.

I got a work around for this by doing this:

document.getElementById("abc").click();
var t=setTimeout(function(){document.getElementById("def").click();},3000)

By putting a waiting time of 3 seconds. But I don't like my approach. This approach depends on the fact that the other page would get loaded by this time. Is there a better way to do this? Can I check whether page 2 was loaded and then perform the action?

Thanks

Vivek
  • 153
  • 1
  • 2
  • 9
  • 3
    When the site is unloaded the javascript is no more active. You cannot access elements in other pages from the page where you clicked the link. Or are you developing a browser extension? – Bergi Oct 05 '13 at 20:29
  • maybe you ran an ajax call somewhere in there? if so that could cause you problems because ajax is asynchronous by default http://stackoverflow.com/questions/2035645/when-is-javascript-synchronous – cranberry Oct 05 '13 at 20:47
  • @Bergi I am using chrome's shortcut manager to do this. I'm technically able to do this but I don't like the number 3000 being there. – Vivek Oct 05 '13 at 21:17
  • In what environment is the JS executed? The plugin description sounds like they were just bookmarks. If you have access to the chrome tabs, there are navigation events which you could use – Bergi Oct 06 '13 at 12:42

0 Answers0