I do apologise for not knowing a single thing about JavaScript.

I am under the impression that I can have a code to handle events in a webpage header.
I am specicically reffering to Internet Explorer Browser.
My goal it to capture and handle the event BeforeNavigate2.
I would be examining the URL destination and optionally asking a user to confirm
navigation and conditionally canceling it.

I would really appreciate any direction anyone could offer.

Thanks.

Dani AI

Generated

As noted, the navigation hook sought is a host/container event — DWebBrowserEvents2::BeforeNavigate2 — which the embedded Internet Explorer/WebBrowser control fires before a navigation starts. The container can inspect the destination URL and cancel the navigation there (this is the most robust place to enforce allow/deny rules). (learn.microsoft.com)

When using AutoIt the usual pattern is to attach an event sink with ObjEvent and handle the DWebBrowserEvents2 BeforeNavigate2 callback. The example below shows the minimal wiring and a simple blocking check; because AutoIt has historically had trouble reliably setting the Cancel parameter in some builds, the handler also calls Stop on the pending disp object as a practical fallback.

#include <IE.au3>

$oIE = _IECreate("about:blank")
$hEvt = ObjEvent($oIE, "_IEEvent_", "DWebBrowserEvents2")
_IENavigate($oIE, "http://example.com")

While 1
    Sleep(100)
WEnd

Func _IEEvent_BeforeNavigate2($pDisp, $Url, $Flags, $TargetFrameName, $PostData, $Headers, $Cancel)
    If StringInStr($Url, "blockeddomain.com") Then
        $Cancel = True    ; attempt to cancel (may not be reliable in some AutoIt builds)
        $pDisp.Stop       ; pragmatic fallback that reliably halts the pending navigation
    EndIf
EndFunc

ObjEvent and the IE UDF are the supported AutoIt entry points for this pattern. Note the Cancel-by-reference behavior has caused issues for some AutoIt versions; forum/trac threads document that setting Cancel may not always work and show the Stop() workaround used in practice. (autoitscript.com)

Injecting page-level JavaScript (event delegation or jQuery) can still be useful for intercepting plain user clicks (as suggested), but it has limits: programmatic navigations (location changes, form POSTs, scripts) and cross-origin iframes will often bypass injected handlers or be unreachable due to same-origin restrictions. A single delegated document-level click handler plus form submit hooks covers many cases, but frame-by-frame injection is required for same-origin frames and it won’t cover all programmatic navigations. For the browser-side APIs and what can/cannot be intercepted see the Navigation API and preventDefault notes. (developer.mozilla.org)

Recommendation: prefer the host BeforeNavigate2 handler when embedding IE (inspect URL and stop or reroute there). If building a new embed, consider a modern host (WebView2) that exposes NavigationStarting with explicit Cancel semantics for reliable interception. (learn.microsoft.com)

Recommended Answers

All 8 Replies

That event is only triggered if you use the WebBrowser object from MS. It does not work if you are using a regular browser.

I was unaware of that, but yes, that is what I am using in a GUI to create a custom browser.

If you tell me what language you are using, then I'll move this thread to the right forum. Usually, if you are using an IDE, then you would have this event available somewhere.

I can capture the Event in the language I am using "AutoIt" but I do not know the JavaScript I need to inject into the header of a page to have it handled before navigation takes place.

I can capture and cancel a navigation by the onclick event by injecting "return false;"
But then every link would need a handler added for it, and I don't want to get into
iterating through every iframe on every page and adding code for every link I find.

The exact problem from the exact page, from which I am trying to find a solution.
Which I think lies in the Javascript which is added to the header of the page.

I can capture and cancel a navigation by the onclick event by injecting "return false;" but ...

You may not want to do this but it is by far the easiest approach, especially with the help of the jQuery lib, which would allow you very simply to attach an onclick handler to all nav links with one statement per page/iFrame, regardless of the number of nav links.

Install jQuery on the page as follows:

<script src="path/to/jQuery"></script>

Then, you need a function to apply your filtering rules. Here's a generalised template :

function navFilter() {
    var $this = $(this);
        href = $this.attr('href'),
        linkText = $this.text();
    if(href ..... ) {//test for urls you wish to reject unconditionally
        //here display rejection message if required
        return false;
    }
    if(href ..... ) {//test for urls you wish to reject with user sanction
        return confirm("Allow navigation to " + linkText);//or href or both, whichever is more meaningful to your users
    }
    return true;//allow navigation to all urls that fall through the tests
}

Then, attach the handler to all nav links on the page with eg :

$(function() {
    $("#header").on('click', 'a.nav', navFilter);
});

where your header container is identified with id="header" and each nav link is identified with class="nav" (or similar).

You may well need to modify this to make it compaltible with your HTML, but as you can see, attaching a click handler to multiple elements does not have to be onerous.

To apply to several pages, put the code (the filter function and the attachment statement) into its own navfilter.js file and include on each page with another <script> tag (after the jQuery script tag) :

<script src="path/to/navfilter.js"></script>

Thank you Airshow, I will study your code and impliment it.
Much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.