Skip to content

WebView2 Control Core Features Reference

  • Description: Navigate to a specified URL
  • Syntax: Navigate(ByVal Url As String)
  • Example:
vb
' Navigate to a website
WebView21.Navigate "https://www.example.com"

' Navigate to a local HTML file
WebView21.Navigate "file:///C:/web/index.html"

LoadHtml Method

  • Description: Load HTML string content
  • Syntax: LoadHtml(ByVal htmlContent As String)
  • Example:
vb
' Load simple HTML content
WebView21.LoadHtml "<html><body><h1>Hello World</h1></body></html>"

' Load HTML content with styling
Dim html As String
html = "<html>" & _
       "<head><style>body {font-family: Arial; color: blue;}</style></head>" & _
       "<body><h1>Title</h1><p>This is a paragraph</p></body>" & _
       "</html>"
WebView21.LoadHtml html

GoBack / GoForward Methods

  • Description: Navigate backward/forward in browser history
  • Related Properties:
    • CanGoBack As Boolean - Whether backward navigation is possible
    • CanGoForward As Boolean - Whether forward navigation is possible
  • Example:
vb
Private Sub cmdBack_Click()
    If WebView21.CanGoBack Then
        WebView21.GoBack
    End If
End Sub

Private Sub cmdForward_Click()
    If WebView21.CanGoForward Then
        WebView21.GoForward
    End If
End Sub

Reload Method

  • Description: Reload the current page
  • Example:
vb
' Refresh current page
WebView21.Reload

' Example: Create refresh button
Private Sub cmdRefresh_Click()
    WebView21.Reload
End Sub

Page Information

DocumentURL Property

  • Description: Get the current document's URL
  • Return Type: String
  • Example:
vb
' Get and display current URL
Private Sub ShowCurrentUrl()
    Debug.Print "Current page URL: " & WebView21.DocumentURL
End Sub

DocumentTitle Property

  • Description: Get the current document's title
  • Return Type: String
  • Example:
vb
' Update window title to current page title
Private Sub UpdateWindowTitle()
    Me.Caption = WebView21.DocumentTitle & " - My Browser"
End Sub

JavaScript Interaction

ExecuteScript Method

  • Description: Execute JavaScript code without waiting for result
  • Syntax: ExecuteScript(ByVal jsCode As String)
  • Example:
vb
' Execute simple JavaScript code
WebView21.ExecuteScript "alert('Hello from VB!')"

' Modify page content
WebView21.ExecuteScript "document.body.style.backgroundColor = 'lightblue';"

' Call a function in the page
WebView21.ExecuteScript "updateContent('New content');"

JsRun Method

  • Description: Execute JavaScript function and wait for result
  • Syntax: JsRun(ByVal FuncName As String, ParamArray args() As Variant) As Variant
  • Example:
vb
' Call function without parameters
Dim result As Variant
result = WebView21.JsRun("getCurrentTime")

' Call function with parameters
Dim sum As Variant
sum = WebView21.JsRun("add", 5, 3)  ' Calls add(5, 3)

' Get page element content
Dim content As String
content = WebView21.JsRun("getElementContent", "myDiv")

JsRunAsync Method

  • Description: Execute JavaScript function asynchronously
  • Syntax: JsRunAsync(ByVal FuncName As String, ParamArray args() As Variant) As LongLong
  • Example:
vb
' Async function call
Dim token As LongLong
token = WebView21.JsRunAsync("longRunningOperation", "param1")

' Handle result in JsAsyncResult event
Private Sub WebView21_JsAsyncResult(ByVal Result As Variant, _
    Token As LongLong, ErrString As String)
    If Token = savedToken Then  ' savedToken is previously stored token value
        If ErrString = "" Then
            Debug.Print "Async operation result: " & Result
        Else
            Debug.Print "Error: " & ErrString
        End If
    End If
End Sub

AddObject / RemoveObject Methods

  • Description: Expose/Remove COM objects to/from JavaScript engine
  • Syntax:
    • AddObject(ByVal ObjName As String, ByRef Object As Object, ByVal UseDeferredInvoke As Boolean = False)
    • RemoveObject(ByVal ObjName As String)
  • Example:
vb
' Create class to expose to JavaScript
Private Type TCalcHelper
End Type

Private Sub Class_Initialize()
    ' Initialization code
End Sub

Public Function Add(ByVal a As Long, ByVal b As Long) As Long
    Add = a + b
End Function

' Expose object in WebView2 control
Dim calc As New CalcHelper
WebView21.AddObject "calculator", calc, False

' JavaScript usage example:
' let result = chrome.webview.hostObjects.calculator.Add(5, 3);
' console.log('Calculation result:', result);

' Remove object
WebView21.RemoveObject "calculator"

PostWebMessage Method

  • Description: Send messages to JavaScript
  • Syntax: PostWebMessage(Message As Variant)
  • Example:
vb
' Send string message
WebView21.PostWebMessage "Hello from VB"

' Send complex data
Dim data As String
data = "{""type"": ""update"", ""value"": 42}"
WebView21.PostWebMessageJSON data

' JavaScript receiving code:
' window.chrome.webview.addEventListener('message', function(event) {
'     console.log('Received message:', event.data);
' });

Base on VB6 component release