WebBrowser Control and the NewWindow2 event

My webbrowser control loads a page from a secure site. Some of the popup links on that page are located within that site and I must load them in a new instance of the webbrowser or I will be presented with another logon dialogue. Other links are off site (unsecured) and can be opened in a new instance of IE. My preference would be to open all popup links in IE.

My question is, how can I determine which link has been clicked before the NewWindow2 event? The BeforeNavigate2 is not called before a popup window. Even better would be to pass all popup windows to IE but I can't figure out how to do this without receiving a logon dialogue.
Who is Participating?
vb_elmarConnect With a Mentor Commented:
This sample needs:
1 button
1 webbrowser control

On a form, add a webbrowser1 control (CTRL-T \ "Microsoft Internet Controls")

Go to the VB toolbox, and  click on the blue globe icon. Paste
the MSIE ActiveX Control onto the form.

-Paste the code below into your Form.

Run it.

'Form Code:

Private Sub Command1_Click()
    WebBrowser1.Navigate "about:blank"
Call wait_4_navigate
    zz = ""
    zz = zz & "<A href='http://whatismyip.com' target=_blank>target1</A>"
    zz = zz & "<br>"
    zz = zz & "<br>"
    zz = zz & "<A href='http://spotlight.de/zforen/mvb/m/mvb-1146230885-13311.html' target=_blank>target2</A>"
    WebBrowser1.Document.write zz
End Sub

Private Sub Form_Load()
    BackColor = RGB(166, 166, 144)
    Move 0, 0, Screen.Width * 0.98, Screen.Height * 0.94
    Command1.Move 0, 0
    WebBrowser1.Move 0, Command1.Height * 2, Width * 0.95, Height * 0.8
End Sub

Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    Caption = URL
    If Len(URL) > 8 Then Caption = URL
    If URL = "http://whatismyip.com/" Then
        Cancel = True
        'WebBrowser1.Navigate "about:blank"
        'Call wait_4_navigate
        a = MsgBox("close this webbrowser window ?", 4, "blocked: whatismyip.com")
        If a = vbYes Then Visible = 0
    End If
End Sub

Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel As Boolean)
    Dim f       As New Form1
    f.WebBrowser1.RegisterAsBrowser = True
    Set ppDisp = f.WebBrowser1.Object
    f.Visible = True
End Sub

Sub wait_4_navigate()
    a = WebBrowser1.ReadyState
    If a <> myState Then myState = a: Me.Caption = Me.Caption & "      readystate-->:" & a
    Loop While a <> myState Or a <> 4
    Caption = Caption & "      ***READY***"
End Sub

Can you post the URL of the site?
dbalmanAuthor Commented:
No, it's internal.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

In order to get an answer for this question it would be helpful, if we get the link
of a comparable http site, because this is a VB expert forum (not a html expert forum).
dbalmanAuthor Commented:
This is not a HTML question. Please reread the question. If further clarification is required then please ask.
When writing a code for your problem, a sample http site would be helpful for testing purposes. At this
moment, a testing site is not available, therefore i would need to program the testing site by myself (e.g. with
frontpage). But to write a site by myself is too time-consuming for me.
dbalmanAuthor Commented:
Maybe this will help.  I need the URL being navigated to before the NewWindow2 event.

Private Sub Form_Load()

WebBrowser1.Navigate2 "About: Blank"

WebBrowser1.Document.write "<A href=""http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/"" target=_blank>http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/</A><DIV></DIV><A href=""http://www.google.com/""target=_blank>http://www.google.com</A>"
End Sub

I can't provide a secure link as that would entail providing the account to access that site also. I will max the points if someone does figure out how to open https popups (located in a webbrowser control) in IE.

dbalmanAuthor Commented:
At first I couldn't figure out why your solution was firing the forms NewWindow2 event and mine wasn't. It then it occurred to me that I am using a separate form and webbrowser to handle pop-ups. I need to watch its NewWindow2 event then I can send it to IE or a new pop-up form. I overlooked the obvious and I appreciate your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.