2017-07-14 88 views
0

我有一個VB.Net Windows應用程序項目。VB.Net WebBrowser調用JavaScript函數不起作用

這是下面的VB.Net代碼片段。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    'webBrowser1 is the variable of the System.Windows.Forms.WebBrowser class. 
     webBrowser1.Navigate("file:///" & IO.Path.GetFullPath(".\test.html")) 

     'not working 
     webBrowser1.Document.InvokeScript("test") 

這是下面的test.html代碼片段。

<html> 
<title>test</title> 
<body> 
<script> 
    function test(){ 
     alert('test'); 
    } 
</script> 
</body> 
</html> 

此應用程序不會在javascript測試函數中彈出「測試」警報。

如何解決這個問題?

+0

我通常用它來調用腳本:'ClientScript.RegisterStartupScript(Me.GetType(),「 RunScriptRun「,」「)'。讓我知道它是否適用於你的情況。 –

+0

@AllenBinuya:只適用於ASP.NET,不適用於WinForms。 –

+0

哦..對不起,我無法區分WinFoms和ASP.NET。 –

回答

0

我已經知道problem.After移動InvokeScript功能button_click功能,它的working.So這是通話計時問題

+0

啊,當然。是的,你必須給頁面加載的機會。如果要在加載頁面後執行某些操作,可以使用[**'DocumentCompleted' **](https://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser.documentcompleted( v = vs.110).aspx)事件。當Web瀏覽器完成加載頁面時會引發此問題。 –