Capturing Javascript Alert in Webbrowser Control

我是研究僧i 提交于 2019-12-02 17:17:51

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1609150/capturing-javascript-alert-in-webbrowser-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!