EdLost

Hey all,

I'm trying to do a simple thing in .NET (C#). I'm trying to change the url that the browser goes to using the BeginNavigate2 event. My code looks like this:

static void OnBeforeNavigate2(Object ob1, ref Object URL, ref Object Flags, ref Object TargetFrameName,

ref Object PostData,ref Object Headers, ref bool Cancel)

{

Console.WriteLine("In Before Navigate2");

URL = "www.yahoo.com";

Cancel = false;

}

The event fires but the url the browser goes to is never 'www.yahoo.com'. This is code is a simple modification of the SDK sample for internet explorer interop. If I set Cancel to true, the navigation stops so I know the event is called. I can't figure out why, the URL is ignored.



Re: Internet Explorer Extension Development BeginNavigate2 change the url

John Sudds - MSFT

Here's an example in C++ that does what you want to do. It should be fairly simple to convert to VB. (Pay particular attention to the comments...)

case DISPID_BEFORENAVIGATE2:
{
    // Is navigation to specified Url disallowed
    ATLASSERT((*pDispParams).rgvarg[5].vt = VT_BYREF | VT_BSTR);
    CComBSTR url = ((*pDispParams).rgvarg)[5].pvarVal->bstrVal;
    if (url == "http://www.adatum.com" || url == "http://www.adatum.com/")
    {
        // If so, navigate the browser frame to standard resource page
        CComQIPtr<IWebBrowser2> spBrowser = ((*pDispParams).rgvarg)[ 6 ].pdispVal;
        if (spBrowser != NULL)
        {
            static const CComBSTR newURL = L"res://ieframe.dll/navcancl.htm";
            spBrowser->Navigate(newURL, NULL, NULL, NULL, NULL);
            // Set Cancel parameter to TRUE to cancel the current event
            *(((*pDispParams).rgvarg)[0].pboolVal) = TRUE;
        }
    }
    break;
}

Notice that the event in progress is canceled and Navigate is called again with the desired URL.






Re: Internet Explorer Extension Development BeginNavigate2 change the url

EdLost

Thanks. So a not so simple example has to deal with the recursion since calling navigate from inside the BeforeNavigate2 fires the event again That ought to be fun.