Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
 
any update on your problem? I am facing the same issue. –  Computer User Jun 24 at 18:48
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
 
Any idea how to tie this in with the webbrowser, I don't see how to relate the two. –  CaveManCode Oct 22 '09 at 19:15
 
Not quite sure how to do it with C#. See this KB article: support.microsoft.com/kb/312777 though you want to use the window object rather than the document object (document.parentWindow) –  Andy E Oct 22 '09 at 19:22
 
Hey Andy, I am still hacking at it. Thanks for the direction. –  CaveManCode Oct 22 '09 at 20:25
 
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 Oct 22 '09 at 20:36
 
Cool got the answer. –  CaveManCode Oct 23 '09 at 17:11
show 2 more comments

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.

share|improve this answer
 
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 Oct 22 '09 at 19:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.