mobileFX WebKitX CEF3 ActiveX 4.x
WebKitXCEF3Lib ActiveX Control / WebKitXCEF3 Object / OnBeforeNavigate Event

The HTTP Request identifier.

The Browser identifier.

The Frame identifier.

The Frame name (if any).

Indicates if the navigation is an HTTP redirect.

HTTP Method (POST, GET, etc.)

Get the referrer URL.

The current URL of the Frame.

By setting this argument you can alter the navigation URL programmatically.

By setting this argument to True you can cancel the navigation.

In This Topic
    OnBeforeNavigate Event
    In This Topic
    Description

    Fired before a Frame loads a new URL. The event is classified as synchronous and for performance reasons is disabled by default. Read remarks section below to enable it.

    Syntax
    Visual Basic
    Public Event OnBeforeNavigate( _
       ByVal Id As Long, _
       ByVal BrowserID As Long, _
       ByVal FrameID As Long, _
       ByVal FrameName As String, _
       ByVal IsRedirect As Boolean, _
       ByVal ResourceType As NAV_RESOURCE_TYPE, _
       ByVal TransitionType As NAV_TRANSITION_TYPE, _
       ByVal Method As String, _
       ByVal ReferrerURL As String, _
       ByVal CurrentURL As String, _
       ByRef NewURL As String, _
       ByRef Cancel As Boolean _
    )
    Parameters
    Id

    The HTTP Request identifier.

    BrowserID

    The Browser identifier.

    FrameID

    The Frame identifier.

    FrameName

    The Frame name (if any).

    IsRedirect

    Indicates if the navigation is an HTTP redirect.

    ResourceType
    ValueDescription
    RT_CSP_REPORTA report of Content Security Policy violations.
    RT_FAVICONFavicon.
    RT_FONT_RESOURCEFont.
    RT_IMAGEImage (jpg/gif/png/etc).
    RT_MAIN_FRAMETop level page.
    RT_MEDIAMedia resource.
    RT_OBJECTObject (or embed) tag for a plugin, or a resource that a plugin requested.
    RT_PINGA request for a <ping>
    RT_PLUGIN_RESOURCEA resource that a plugin requested.
    RT_PREFETCHExplicitly requested prefetch.
    RT_SCRIPTExternal script.
    RT_SERVICE_WORKERMain resource of a service worker.
    RT_SHARED_WORKERMain resource of a shared worker.
    RT_STYLESHEETCSS stylesheet.
    RT_SUB_FRAMEFrame or iframe.
    RT_SUB_RESOURCESome other sub resource. This is the default type if the actual type is unknown.
    RT_WORKERMain resource of a dedicated worker.
    RT_XHRXMLHttpRequest.
    TransitionType
    ValueDescription
    TT_AUTO_SUBFRAMESource is a subframe navigation. This is any content that is automatically loaded in a non-toplevel frame. For example, if a page consists of several frames containing ads, those ad URLs will have this transition type. The user may not even realize the content in these pages is a separate frame, so may not care about the URL.
    TT_BLOCKED_FLAGAttempted to visit a URL but was blocked.
    TT_CHAIN_END_FLAGThe last transition in a redirect chain.
    TT_CHAIN_START_FLAGThe beginning of a navigation chain.
    TT_CLIENT_REDIRECT_FLAGRedirects caused by JavaScript or a meta refresh tag on the page.
    TT_DIRECT_LOAD_FLAG 
    TT_EXPLICITSource is some other "explicit" navigation action such as creating a new browser or using the LoadURL function. This is also the default value for navigations where the actual type is unknown.
    TT_FORM_SUBMITSource is a form submission by the user. NOTE: In some situations submitting a form does not result in this transition type. This can happen if the form uses a script to submit the contents.
    TT_FORWARD_BACK_FLAGUsed the Forward or Back function to navigate among browsing history.
    TT_IS_REDIRECT_MASKUsed to test whether a transition involves a redirect.
    TT_LINKSource is a link click or the JavaScript window.open function. This is also the default value for requests like sub-resource loads that are not navigations.
    TT_MANUAL_SUBFRAMESource is a subframe navigation explicitly requested by the user that will generate new navigation entries in the back/forward list. These are probably more important than frames that were automatically loaded in the background because the user probably cares about the fact that this link was loaded.
    TT_QUALIFIER_MASKGeneral mask defining the bits used for the qualifiers.
    TT_RELOADSource is a "reload" of the page via the Reload function or by re-visiting the same URL. NOTE: This is distinct from the concept of whether particular load uses "reload semantics" (i.e. bypasses cached data).
    TT_SERVER_REDIRECT_FLAGRedirects caused by JavaScript or a meta refresh tag on the page.
    TT_SOURCE_MASKGeneral mask defining the bits used for the source values.
    Method

    HTTP Method (POST, GET, etc.)

    ReferrerURL

    Get the referrer URL.

    CurrentURL

    The current URL of the Frame.

    NewURL

    By setting this argument you can alter the navigation URL programmatically.

    Cancel

    By setting this argument to True you can cancel the navigation.

    Remarks

    The event is fired on the UI thread before browser navigation of the main frame.

    For performance reasons the event is disabled by default. Intercepting navigation in CEF requires  the browser thread to be blocked until the OLE / COM event is handled, which subsequently requires switching to synchronous events model between the ActiveX and Chromium Embedded Framework sub-processes.

    1. To enable synchronous events you must handle OnCreate event and set Settings.asynchronous_com_events = False.
    2. To enable firing OnBeforeNavigate you must handle OnCreate event and set Settings.disable_on_before_navigate_event = False.
    3. To enable the Cancel flag of the event also requires handling OnCreate and enabling it by setting Settings.allow_cancel_on_before_navigate = True.

    Set Cancel to true to cancel the navigation or false to allow the navigation to proceed. You can also stop a URL navigation that has already started with StopLoading Method.

    You can set a different URL to navigate to by setting the NewURL argument.

    Example
    Option Explicit
    
    Public URL As String
    
    Private Sub Form_Load()
        URL = "https://www.webkitx.com/"
    End Sub
    
    Private Sub Form_Resize()
        On Error Resume Next
        WebKitX1.Move 0, 0, ScaleWidth, ScaleHeight
        Err.Clear
    End Sub
    
    Private Sub WebKitX1_OnCreate(ByVal Settings As WebKitXCEF3Lib.ISettings, CommandLineSwitches As String)
        Settings.asynchronous_com_events = False
        Settings.allow_cancel_on_before_navigate = True
        Settings.disable_on_before_navigate_event = False
    End Sub
    
    Private Sub WebKitX1_OnBrowserReady()
        WebKitX1.Open URL
    End Sub
    
    Private Sub WebKitX1_OnBeforeNavigate(ByVal ID As Long, ByVal BrowserID As Long, ByVal FrameID As Long, ByVal FrameName As String, ByVal IsRedirect As Boolean, ByVal ResourceType As WebKitXCEF3Lib.NAV_RESOURCE_TYPE, ByVal TransitionType As WebKitXCEF3Lib.NAV_TRANSITION_TYPE, ByVal Method As String, ByVal ReferrerURL As String, ByVal CurrentURL As String, NewURL As String, Cancel As Boolean)
    
        If NewURL = URL Then
            
            ' Ask the user what to do with the URL
            Dialog.Label1 = "Load URL?" + vbCrLf + NewURL
            Dialog.Show vbModal
            Cancel = Not Dialog.Result
            
        End If
        
        Unload Dialog
    
    End Sub
    See Also