Hi there...

How can you stop IE from opening up in a WebBrowser control on a new window (target=_blank")? I've gotten the WebBrowser_NewWindow thig and tried e.Cancel and CancelEventArgs and none of these work. What do I do?

By THe way, I'm using VB Express 2008

Please help me! This is the only thing I have left to do! Success is like a step away!

Recommended Answers

All 8 Replies

How can you stop IE from opening up in a WebBrowser control on a new window (target=_blank")?

If I understand correctly, you put a WebBrowser control on a form, and when you Issue Navigate it open in IE instead of the form? if that so,

WebBrowser1.Navigate("www.google.com", False)

A bullet-proof method is

WebBrowser1.AllowNavigation = False

but that does prevent all navigation. That's probably what you don't want.

You could trap NewWindow event (like you did)

Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
  ' Prevent opening a new windows
  e.Cancel = True

End Sub

and that worked for me. I tested it with a DaniWeb's thread. "Help with Code Tags" have target="_blank" and they were blocked to open in IE.

It's a good thing to keep in mind that there are many ways to open a pop-up window from a web page. I believe that you just can't prevent all pop-ups with the WebBrowser control.

A bullet-proof method is

WebBrowser1.AllowNavigation = False

but that does prevent all navigation. That's probably what you don't want.

You could trap NewWindow event (like you did)

Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
  ' Prevent opening a new windows
  e.Cancel = True

End Sub

and that worked for me. I tested it with a DaniWeb's thread. "Help with Code Tags" have target="_blank" and they were blocked to open in IE.

It's a good thing to keep in mind that there are many ways to open a pop-up window from a web page. I believe that you just can't prevent all pop-ups with the WebBrowser control.

I tried e.Cancel, but it says that cancel isn't part of System.EventArgs . I think it's because you've already clicked, and the process has already started.

it says that cancel isn't part of System.EventArgs

That's correct. But in the code I posted, parameter e is defined ByVal e As System.ComponentModel.CancelEventArgs .

I think it's because you've already clicked, and the process has already started.

That's correct too. And e.Cancel = True cancels that.

I have .NET 2.0 and I haven't used .NET 1.x version. Is your WebBrowser control 1.x version perhaps? That could explain why you have argument e of different type than I have. I believe that in .NET 3.x version you would also have ByVal e As System.ComponentModel.CancelEventArgs in WebBrowser1.NewWindow event. That's the only thing that comes in to my mind.

That's correct. But in the code I posted, parameter e is defined ByVal e As System.ComponentModel.CancelEventArgs .
That's correct too. And e.Cancel = True cancels that.

I have .NET 2.0 and I haven't used .NET 1.x version. Is your WebBrowser control 1.x version perhaps? That could explain why you have argument e of different type than I have. I believe that in .NET 3.x version you would also have ByVal e As System.ComponentModel.CancelEventArgs in WebBrowser1.NewWindow event. That's the only thing that comes in to my mind.

Oh my god!
That would explain so much! I never saw that. I'll try at as soon as I get home. Thanks, I have a feeling it'll work now! (Knock on virtual wood). THANKS SO MUCH!

Thanks! It worked! Like a charm!

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.