VB.NET webbrowser - force popup window to navigate in the same window

I have a webbrowser control. If I click on a popup window of a webpage that is loaded, I want the new window event to be canceled and the webbrowser should just navigate to the target URL of the new window within the webbrowser control of my app.

I found this resource, yet can not make sense of it.

Is there some example code?
wfskmoneyAsked:
Who is Participating?
 
kristof1104Commented:
Just made a test proj here is the code:

Public Class Form1

    Dim url As String = ""

    Private Sub WebBrowser1_NewWindow(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
        e.Cancel = True
        WebBrowser1.Navigate(url)
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("file://C:\Users\kve\Desktop\bla.html")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim links As HtmlElementCollection = WebBrowser1.Document.Links
        For Each var As HtmlElement In links
            var.AttachEventHandler("onclick", AddressOf LinkClicked)
        Next
    End Sub

    Private Sub LinkClicked(sender As Object, e As EventArgs)
        Dim link As HtmlElement = WebBrowser1.Document.ActiveElement
        url = link.GetAttribute("href")
    End Sub
End Class

Open in new window


I can also upload the project for you if you want
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
ArkCommented:
For Each var As HtmlElement In links
Your need check ALL elements onclick events as well - it can point to javascript:window.open()
BTW, for links you can use following dirty trick:
Private Sub WebBrowser1_NewWindow(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
        e.Cancel = True
        WebBrowser1.Navigate(WebBrowser1.StatusText)
    End Sub

Open in new window

:)
0
 
wfskmoneyAuthor Commented:
The javascript workarounds do not work for sites that use other methods to open pop up windows.

For my project, I have an inline browser where users can donate to savethechildren.com

However, for checking that the user has successfully donated, I need to keep the paypal button popup inside the webbrowser control. If the paypal buttons opens a new window, I can not access the content of the landing page after the donation.

I managed to build a project where popups are opened in a second tab (a new webbrowser object is created for that).

It works for normal popups but not for paypal buttons. You can test it by clicking on the paypal button of the site that loads. Anyone have an idea?

NewWindowinBrowserControl
0
 
ArkCommented:
See VB version of extended webbrowser:
http://code.bn2vs.com/viewtopic.php?t=83
0
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.