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.


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.