2012-11-28 26 views
0

我認爲InvokeScript可能工作,但它沒有。是否有可能在AutoHotkey中隨時執行加載到WebBrowser控件中的JavaScript函數?

strHTML = 
(
<!DOCTYPE html> 
<html> 
    <head> 
     <script> function displayDate() { document.getElementById("demo").innerHTML=Date(); }</script> 
    </head> 
    <body> 
     <p id="demo" onclick="displayDate()">This is a paragraph. Click here.</p> 
    </body> 
</html> 
) 

new WebBrowser(strHTML) 

Class WebBrowser 
{ 
    __New(strHTML) { 
     static WB 
     Gui, New, Resize 
     Gui, Add, ActiveX, vWB w780 h580 , Shell.Explorer 
     Gui, show, w800 h600 

     WB.Navigate("about:blank") 
     Loop 
      Sleep 10 
     Until (WB.readyState=4 && WB.document.readyState="complete" && !WB.busy)  

     WB.Document.Write(strHtml) 
     ; WB.Document.Close() 

     WB.document.InvokeScript("displayDate") 
     ; WB.document.parentWindow.document.InvokeScript("displayDate") ; does not work 
     return this 

     GuiClose: 
     ExitApp 
    } 
} 

回答

2

至少有兩種方法,你可以調用函數:在腳本中

WB.document.parentWindow.execScript("displayDate()") 
WB.document.parentWindow.displayDate() 

全球所有功能都可以作爲window對象的方法。

您鏈接到文章中所指的WebBrowser控件是.NET Framework的一部分,是不一樣的,你正在使用WebBrowser控件。這就是爲什麼試圖調用InvokeScript提出了一個「未知的名稱」錯誤消息。

相關問題