Keith Twombley
Keith Twombley

Reputation: 1688

Determine if browser is navigating away - without onunload

I am injecting javascript into a page via autohotkey by entering javascript:blahblahblah into the location bar.

I need to determine if IE is waiting to navigate to a new page (e.g. page status is spinning, but it hasn't refreshed to the new page yet).

Currently I'm using document.readyState, however, there is sometimes a long delay before the remote webserver replies. During that delay, readyState still says "complete" (because it is complete, it's just the old page, not the new one)

If I try to do something to the page, it will be operating on the old page, rather than the upcoming one.

I could just sleep for a couple of minutes after each page navigation, but that would make the script take forever.

Hooking onunload won't work via the addressbar, since each time I enter something in the address bar it will trigger the onunload, causing tons of false-positives.

Is there some way in javascript on IE to tell if the browser is waiting to go to a new page?

Upvotes: 0

Views: 377

Answers (1)

What a coincidence, I was just looking around for the same issue,I used to use readyState and faced your problem, I found a similar event handlers in VB like you also tried to use but as you said it will execute every time event is triggered.

Sol: I looked up in MSDN and found Busy property which gets a value that indicates whether the object is engaged in a navigation or downloading operation, now I use it in addition to readyState, the Busy is for waiting remote server to reply and readyState is for the object to be ready to take actions with it.

notice: I tried to use Busy alone but some times the reply is too fast and the next instruction tries to be executed before the object is ready.

Example in AHK (test login page 20 times at high rate):

Loop, 20
{
Global IE := ComObjCreate("InternetExplorer.Application")
IE.visible := 1
IE.Silent := 0
Login:
IE.navigate("www.example.com")
While (IE.ReadyState != 4 or IE.Busy = True)
continue

IE.document.forms("login").name.value := "Username"
IE.document.forms("login").password.value := "Password"
;IE.document.forms("login").s1.Click()
IE.document.forms("login").Submit()
While (IE.ReadyState != 4 or IE.Busy = True)
continue

;sleep 1000 ;use it just to see that you are login successfully 
IE.Stop()
IE.Quit()
}

Upvotes: 1

Related Questions