1

OnBeforeNavigate2 event occurs multiple times for a single document. The document I am loading contains iframe so that triggers OnBeforeNavigate2 event multiple times.

What I want to do is to figure out which frame triggered it and cancel the navigation if frame triggered it, and not a user-click.

I am somewhat sure that I need to handle pDisp parameter to determine the frame source and if it is with NULL parent or with browser parent - code would do something like that:

void __fastcall TForm1::EmbeddedWBBeforeNavigate2(TObject *ASender, const IDispatch *pDisp,
          OleVariant &URL, OleVariant &Flags, OleVariant &TargetFrameName, OleVariant &PostData,
          OleVariant &Headers, WordBool &Cancel)
{
// This code is supposed to do that... (not in C++)
//var thisBrowser = pDisp as SHDocVw.WebBrowser;
//var parent = thisBrowser.Parent as SHDocVw.WebBrowser;
//bool isFrame = (parent == thisBrowser || parent == null);
...
}

I need help in figuring out the above and translating to C++ Builder. If the above is the solution. Or if it is not - the way to figure out how to determine if frame or iframe triggered this event or the user-click.

Update: (for future googlers)

I found some other solutions to this:

bool IsFrame = (EmbeddedWB->ControlInterface != pDisp);

Original post - How do I avoid the OnDocumentComplete event for embedded iframe elements?

Community
  • 1
  • 1
Coder12345
  • 3,431
  • 3
  • 33
  • 73

1 Answers1

1

Try this:

void __fastcall TForm1::EmbeddedWBBeforeNavigate2(TObject *ASender, const IDispatch *pDisp,
          OleVariant &URL, OleVariant &Flags, OleVariant &TargetFrameName, OleVariant &PostData,
          OleVariant &Headers, WordBool &Cancel)
{
    _di_IWebBrowser thisBrowser = pDisp;
    _di_IWebBrowser parent = thisBrowser->Parent;
    bool isFrame = ((!parent) || (parent == thisBrowser));
    ...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you for your reply - it didn't work for me but it might for someone else, I found another solution though and updated the question. I also think this should use `_di_IWebBrowser2` interface. – Coder12345 Apr 08 '16 at 17:23