Capturing Javascript Alert in Webbrowser Control

后端 未结 2 1070
失恋的感觉
失恋的感觉 2021-01-27 04:59

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.

相关标签:
2条回答
  • 2021-01-27 05:44

    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.

    0 讨论(0)
  • 2021-01-27 05:51

    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.

    0 讨论(0)
提交回复
热议问题