Hello,
I try to cancel IE navigation by using BHO written in ATL (I use as example a PopupBlocker by codeproject) and to make IE to navigate to "my" url. But Stop()-Method returns the program to Invoke(...) and causes BEFORENAVIGATE2 event again instead of to stop navigation. The IE will be navigated to navigation cancel site and then to http:///.
That is a code example that I use:
STDMETHODIMP CPub::
Invoke(DISPID dispidMember, REFIID riid, LCID lcid, WORD wFlags,DISPPARAMS* pDispParams, VARIANT* pvarResult,EXCEPINFO* pExcepInfo, UINT* puArgErr)
{
if (!pDispParams)
return E_INVALIDARG;
switch (dispidMember)
{
case DISPID_BEFORENAVIGATE2:
*V_BOOLREF(&pDispParams->rgvarg[0])=VARIANT_TRUE;
m_spBrowser->Stop();
// then do m_spBrowser->Navigate2(...);
// but Stop() returns the program to BEFORENAVIGATE2 event
break;
//....
}
What is wrong with it
Thanks

Problem with cancel IE navigation by using Stop() method
rathlar
I have detected by debugging that this problem occurs if navigated link is a frame one.
What can I do to cancel frame navigation
Michell Cronberg
<<<<<First of all, you shouldn't be calling Stop in the BeforeNavigate2 handler>>>>>
Is the sample given in CodeProject wrong Is the above true for IE6 also I am using the same type of code and it seems to be working in IE6. Only in IE7, I am facing some issues which I am not sure is because of this.
Can you please give some code sample of how you would implement this i.e., stopping the navigation in the main browser and navigating to a different URL ThanksRamanujam Sampath
lrryklly
Hi,
First of all, you shouldn't be calling Stop in the BeforeNavigate2 handler. You should set bCancel to true (the dispparams[0] argument).
As for frame pages, the events are all fired for each frame. To interact with the specific frame you are receiving the event for, queryinterface the pDispatch argument of BeforeNavigate2 for an IWebBrowser2 and use that one. To see if you are in a frame or the main page itself, compare the IWebBrowser2 you get from the Dispatch to the one you already have.
-Reza
tkrasinger
The reason you are seeing the function jump to the top when you hit stop, is because Stop is invoking you're IDispatch again. Therefore, your Invoke code gets called again. And then your switch picks up the BeforeNavigate2 caused by stop, and tries to stop it again.
You should be filtering out the URL before calling Stop. Then, when the stop call runs through the BeforeNavigate2 code you have, it will just return, and then your code should advance to the Navigate2.
Try narrowing down the code in either of these 2 ways:
...
case DISPID_BEFORENAVIGATE2:
if ( URL == SomeSpecificUrlYouWantToRedirect )
{
...
spBrowser2->Stop(); // This only calls stop and redirects on a specific URL, otherwise it just takes the default behavior
....
}
or the other method:
...
case DISPID_BEFORENAVIGATE2:
if( URL != TheNavCancelUrl )
{
...
spBrowser->Stop(); // All URLs except the NavCancel ones will be stopped/redirected.
...
}
I hope that clears it up. You are basically just recursively going into an infinite loop since Stop implicitely fires a BeforeNavigate2.
- Reza
Maheswar
I'm sorry, I misunderstood the original post. If your intention is to redirect, then Stop is okay. If you are just trying to cancel the navigation, it is better to use *cancel parameter as this will allow IE to continue to set all its internal state variables. Otherwise, if you just call Stop and don't do anything else, the ReadyState property could potentially be in an undefined state, along with some other internal state variables.
If you call Stop inside BeforeNavigate2, make sure you call some other form of navigation as well (Navigate2/GoBack/Refresh/etc...)
Otherwise, use the *cancel argument.
an example BeforeNavigate2 handler that blocks certain urls could look like this:
if (URL == SomeUrlWeWantToBlock)
{
*cancel = true;
}
An example that Redirects could look like:
if (URL == SomeUrlWeWantToRedirect)
{
CComQIPtr<IWebBrowser2,IID_IWebBrowser2> spBrowser = pDisp;
spBrowser->Stop();
spBrowser->Navigate2( NewUrl, ... );
}
Sorry about the confusion,
Reza
dvh
Thanks for the clarification. That clears up things.
Dhil
thank you very much for your replay.
But the problem stays unfortunately.
I do:
STDMETHODIMP myObject::Invoke(...)
{
.....
switch (dispidMember)
case DISPID_BEFORENAVIGATE2:
*V_BOOLREF(&pDisp_Params->rgvarg[0]) = VARIANT_TRUE;
CComQIPTR <IWebBroser2, IID_IWebBrowser2> spBrowser2 = pDispParams->rgvarg [ 6 ].pdispVal;
spBrowser2->Stop();
// on this place the program returns to case DISPID_BEFORENAVIGATE2 and
// the url to navigate to is res://ieframe.dll/navcancl.htm
// after this navigation will be stoped too, the url is http:///
spBrowser2->Navigate(...);
....
break;
}
Why does the program not execute the next instruction after Stop() but returns in loop back
Because of frames Or is it really new IE 'behavior' after that method
In IE6 Stop() causes really a stop of navigation.
Thanks in advance.