Results 1 to 6 of 6

Thread: Web Browser - Handles NewWindow

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Web Browser - Handles NewWindow

    Hi I have created a web browser and am trying to get the link that is clicked that would normally open in a new window to open in the current window as I do not want to open any other windows or tabs. This is the code I have so far and it seems to work well kind of anyway, it opens the last url not the new one. Sometime it will open just an advert, it seems to open what ever link was last loaded on the site. Here is the code:

    Code:
    Private newURL As String
    
        Private Sub browser_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles browser.Navigating
    
            newURL = e.Url.ToString
    
        End Sub
    
        Private Sub Browser_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles browser.NewWindow
    
    
            'MessageBox.Show(newURL)
            browser.Navigate(newURL)
            e.Cancel = True
        End Sub
    I got the code from this thread http://www.vbforums.com/showthread.p...wser+newwindow

    Anyone know what I am doing wrong.

    Should I not be using the
    Code:
    browser.navigating
    or the
    Code:
    e.Url.ToString
    ie is there something else I should be using?

  2. #2

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Re: Web Browser - Handles NewWindow

    I am starting to think that the code
    Code:
    Private Sub Browser_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles browser.NewWindow
    
    
            'MessageBox.Show(newURL)
            browser.Navigate(newURL)
            e.Cancel = True
        End Sub
    Is running before
    Code:
    Private Sub browser_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles browser.Navigating
    
            newURL = e.Url.ToString
    
        End Sub
    Is that what I am doing wrong?

  3. #3
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Web Browser - Handles NewWindow

    Your problem has a very easy answer. Read up on the MSDN documentation for webbrowser.navigate() method. You will find that this method has an overload that looks like this : webbrowser.navigate(url as system.uri, newWindow as boolean). So what do you think
    Code:
    webbrowser1.navigate("www.google.com", True)
    would do?
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2009
    Posts
    47

    Re: Web Browser - Handles NewWindow

    Quote Originally Posted by Runesmith View Post
    Your problem has a very easy answer. Read up on the MSDN documentation for webbrowser.navigate() method. You will find that this method has an overload that looks like this : webbrowser.navigate(url as system.uri, newWindow as boolean). So what do you think
    Code:
    webbrowser1.navigate("www.google.com", True)
    would do?
    Sorry I just noticed I did not explain very well what I am wanting to do, I am using the "Navigating" rather than navigate" as I want to get the url that was clicked as the url will come from the webpage not from my code. Here is an example, if using IE or an other normal web browser you visit www.trademe.co.nz (NZ's ebay) and you click on the advertising on the right hand side you will get a popup that takes you to the adverts website. What I want to do with my browser is instead of having a popup I want to cancel it (which is working in my code) and then get that url and navigate to it in the current browser. This is currently kind of working but it seems to run the
    Code:
    browser.Navigate(newURL)
    before it actualy loads the new URL into
    Code:
    newURL
    .

    I am 90% sure if I get the
    Code:
    Private Sub browser_Navigating(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles browser.Navigating
    
            newURL = e.Url.ToString
    
        End Sub
    running before
    Code:
    browser.Navigate(newURL)
    but after it handles a new window it will work, but can't seem to get my head around this.

    Thanks for your help.
    Last edited by nzwogboy; Sep 29th, 2009 at 03:09 PM.

  5. #5
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: Web Browser - Handles NewWindow

    Tough, but not impossible.

    In browser_navigating() set a flag that indicates that you successfully caught the URL. Then check for the flag before doing browser.navigate(newURL)
    Last edited by Runesmith; Oct 1st, 2009 at 07:31 AM. Reason: Didn't read the whole of the message, as usual
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Web Browser - Handles NewWindow

    The actual issue here is that the managed browser control does not properly handle the NewWindow2 event of the underlying webbrowser control. This event has a parameter to allow you to redirect the popup to your own browser window instead of a popup IE instance. For whatever reason (probably compatibility with older IE versions) this functionality does not exist in the browser control as it is now.

    I do have an extended managed browser control that exposes NewWindow2 and you can find that here.

    It allows you to have your own form with a browser control in it intercept new window calls from your main browser, and display everything in your own forms, which of course can give you more control over things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width