BeforeNavigate2 Woes
September 23, 2006
There’s a bug I discovered a couple days ago where the Minibrowser sometimes displays the wrong URL and will jump to the wrong url if the external browser action is invoked (either by clicking the button in the Find pane or by clicking return in the Minibrowser).
This is caused by frame URLs invoking the BeforeNavigate2 event causing the minibrowser to think that its navigating somewhere else when in reality it’s probably just an Iframe loading an ad. The prototype for the function is:
afx_msg void CMiniBrowser::OnBeforeNavigate2(LPDISPATCH lpDisp,BSTR url,short flags,BSTR target,VARIANT FAR* post,BSTR headers,BOOL *cancel) ;
At first, I thought that just looking to see if the target length equaled 0 would be sufficient to detect/distinguish a frame. This is unfortunately not a safe assumption. After combing the internet (with Fact200 no less) and Usenet newsgroups, I was only able to find other people with the same question but no answers. After a whole bunch of trial and error, including using the TopLevelContainer method (which didn’t work!), I settled on the following:
afx_msg void CMiniBrowser::OnBeforeNavigate22(LPDISPATCH lpDisp,BSTR url,short flags,BSTR target,VARIANT FAR* post,BSTR headers,BOOL *cancel) {
IWebBrowser2 *wb;
LPDISPATCH container;
bool isFrame=false;
if (SUCCEEDED(lpDisp->QueryInterface(IID_IWebBrowser2,(void**)&wb)) && wb!=NULL) {
if (SUCCEEDED(wb->get_Parent(&container)) && container!=NULL) {
container->Release();
isFrame=true;
}
wb->Release();
}
}
Side note: WordPress’s editor is absolutely horrendous for formatting code!