Thursday, August 6, 2009

How to call javascript functions from VB.NET and using WebBrowser Control

Today I would like to present about the .NET Web Browser Control and triggering javascript functions from VB.NET Windows Form application.

The class WebBrowser is from .NET Framework Class Library which are available to Microsoft Visual Studio 2008/.NET Framework 3.5, Microsoft Visual Studio 2005/.NET Framework 2.0, .NET Framework 3.0 and Microsoft Visual Studio 2010/.NET Framework 4.0.

This control, WebBrowser enables the developer to navigate web pages inside the windows form.

Some of the methods on WebBrowser control are as followed

and some events are Navigating, Navigated and DocumentCompleted and so on.

Example 1 :
webBrowser1.Navigate(New Uri(address))
Catch ex As System.UriFormatException
End Try

Example 2 :
'WebBrowser1.Navigate(UrlString As String,newWindow As Boolean)
WebBrowser1.Navigate("\mypage.html", False)

Calling JavaScript from the Page in "mypage.html" which is navigating in WebBrowser Control.

Example 1 :
'Calling Javascript Functions with parameters
WebBrowser1.Document.InvokeScript("DoSomething", _
New String() {"param1","param2"})

Example 2 :
'Calling Javascript functions without parameters

Hope this will help.
Thanks and Regards


Reference : MSDN

No comments: