简体   繁体   English

确定浏览器是否正在离开-无需onunload

[英]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. 我通过在位置栏中输入javascript:blahblahblah,通过autohotkey将javascript注入页面。

I need to determine if IE is waiting to navigate to a new page (eg page status is spinning, but it hasn't refreshed to the new page yet). 我需要确定IE是否正在等待导航到新页面(例如,页面状态正在旋转,但尚未刷新到新页面)。

Currently I'm using document.readyState, however, there is sometimes a long delay before the remote webserver replies. 当前,我正在使用document.readyState,但是有时远程Web服务器答复之前会有很长的延迟。 During that delay, readyState still says "complete" (because it is complete, it's just the old page, not the new one) 在该延迟期间,readyState仍然显示“完成”(因为它是完整的,所以它只是旧页面,而不是新页面)

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. 挂钩onunload无法通过地址栏进行操作,因为每次我在地址栏中输入内容时,都会触发onunload,从而导致大量假阳性。

Is there some way in javascript on IE to tell if the browser is waiting to go to a new page? IE上的javascript中是否有某种方法可以判断浏览器是否正在等待进入新页面?

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. 巧合的是,我只是在寻找相同的问题,我曾经使用readyState并面对您的问题,我在VB中找到了类似的事件处理程序,就像您也尝试使用的一样,但是正如您所说的,每次事件触发时它将执行。

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. Sol:我在MSDN中查找,发现Busy属性获得了一个值,该值指示该对象是进行导航还是下载操作,现在除了readyState之外,我还使用它, Busy用于等待远程服务器回复, readyState是使对象准备对其采取行动。

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): AHK中的示例(高速率测试登录页面20次):

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM