views:

526

answers:

2

Using the webbrowswer control to cruise a site. Sometimes errors occur which involve a javascript popup box. I would like to do a couple of things when this happens.

  1. Know when a javascript alert popups up.

I have used the LostFocus event with some success but anytime it losses focus that code runs which is annoying.

  1. I would like to know the exact text that the alert box says.

I am not sure where to find the alert box object or cast it for use in C#. I looked all over the internet and couldn't find it.

Any one any clue?

A: 

If you're looking to only trap script error dialogs that appear, I would recommend trapping the window.onerror DOM event. If you assign a handler for this event, the message, (script) file name and line number are passed as arguments, those are all things shown in the error dialog that pops up. Note that most users have scripting error dialogs switched off by default so it would be wise to honour this if the intended purpose is for a large audience.

I'm not sure if there's an easier way, I've only worked with the old COM WebBrowser component.

Andy E
Any idea how to tie this in with the webbrowser, I don't see how to relate the two.
CaveManCode
Not quite sure how to do it with C#. See this KB article: http://support.microsoft.com/kb/312777 though you want to use the window object rather than the document object (document.parentWindow)
Andy E
Hey Andy, I am still hacking at it. Thanks for the direction.
CaveManCode
Andy, I was able to access onError but that event never fires because a javascript Alert doesn't register as an actual Window error. Thanks for the suggestion as it's taught me more about webbrowser event handling.
CaveManCode
Cool got the answer.
CaveManCode
Glad I could be of some help. I think I slightly misunderstood your question but if I pointed you in the right direction it's a good outcome :)
Andy E
A: 

Just do this:

window.alert = function(txt) {
   // Do something
}

This will allow you to do a callback or anything else you want with the alert text.

RussTheAerialist
i am not injecting javascript. I am not using javascript at all. I just want to capture javascript alerts that happen while using the webbrowser control.
CaveManCode