tags:

views:

224

answers:

2

Hi

The radio recently broke in our bedroom and as a result my missus now listen to various radio stations through her laptop. She moans that visiting various pages and clicking the 'listen' link is a bit of a pain. (Note to self: Must buy new radio!)

In the meantime, I have made a 'radio player' in VB 2008 Express, which is nothing more than 6 buttons down the left hand side of the 'player' I have created and a Web Browser Control on the right hand side.

Clicking each button links to the relevant player of the station she wants to listen to. (Being a newbie to VB and programming, I'm quite proud with what I've achieved so far!!)

Anyway, one station I do link to gives an "Are you sure you want to navigate away from this page" prompt: This one:

http://www.mygoldmusic.co.uk/

Well, thats the homepage of the site anyway, the actual player is here:

(Oops, seems I can only post one link! The actual player opens on-click of the 'listen' button then, sorry to be a pain!)

My question is: Is there a way to suppress this message in VB, or even auto-answer OK somehow?

The other sites I have linked to do not display this message, they just navigate away quite happily. Clicking OK on the prompt is no real hardship either, I hear you say, but in the interests of usability, I would just like it to navigate away from the site/player without prompting.

Remember, I'm using Microsoft Visual Basic 2008 Express Edition. (I say that, because I've come across loads of sites that tell you how to do it with JavaScript, just not VB!)

I've got to the point of thinking it can't be done, but here's hoping!

Any help/advice would be greatly appreciated. And, sorry for the lengthy question. Hope it gives you enough info on what I'm trying to achieve.

Thanks in advance again. J.

A: 

The only way I can think of is to actually modify the DOM of the page in the WebBrowser control. That popup is loading when the "window.onunload" event fires. You should be able to override this behaviour by modifying the DOM.

The HTML document DOM (Document Object Model, essentially an object graph of the page structure) is stored in the WebBrowser.HTMLDocument property. Unfortunately, that specific property isn't available to the .NET version. It IS available to COM however, so through some very ugly and messy code you might be able to suppress the event.

The following code should be able to access the COM property containing the HTML DOM. The type returned is IHTMLDocument2, although you'll note that the class itself will return an object. You might need to add a reference to mshtml.dll to get the IHTMLDocument2 interface access the properties of this in a reasonable way.

Dim domDocument As IHTMLDocument2 = webBrowser.HtmlDocument.DomDocument

You can then access the OnUnload event (which sits on the "window" element, one above the document). Unfortunately, the plot thickens a bit here (I did say it was going to be ugly) because you need to pass a IDispatch object to the onunload event. I've never done this specifically but I found a write-up at the following link that provides some samples and should point you in the right direction: http://blogs.msdn.com/cgarcia/archive/2009/08/28/handling-dom-events-in-a-c-activex.aspx

You should be able to follow a similar approach but simply do nothing in the handler method, which should suppress the javascript alert you are getting.

Ryan Brunner
Hi RyanUsing the code in my WebBrowser1 control returns the error "Type IHTMLDocument2 is not defined"I guess this has something to do with your inclusion that "You might need to add a reference to mshtml.dll to get the IHTMLDocument2 interface access the properties of this in a reasonable way."Not quite sure how to add a reference to mshtml.dll - I'll keep trying to chip away at it though. I'll also see what I can glean from the link you posted.Any further input would be great. If not, that's brilliant and I'll get there in the end!Thanks for your help.Justin.
GettingThere
A: 

Get the handle to the dialog and destroy it. Use FindWindow and send a WM_CLOSE message to it.

Nick Brooks