在Webbrowser控件中捕获Javascript警报 [英] Capturing Javascript Alert in Webbrowser Control

查看:130
本文介绍了在Webbrowser控件中捕获Javascript警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用web浏览器控件来浏览网站。有时发生错误,涉及到一个javascript弹出框。当这种情况发生时,我想做几件事情。

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. 知道一个javascript警报弹出窗口。

我已经使用LostFocus事件取得了一些成功,但任何时候它损失了代码运行的焦点,这是令人讨厌的。

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


  1. 我想知道警报框说明的确切文字。

我不知道在哪里可以找到警报框对象或将其转换为在C#中使用。我看了整个网络,找不到它。

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.

任何一个线索?

推荐答案

只能捕获出现的脚本错误对话框,我建议将 window.onerror DOM事件。如果为此事件分配处理程序,那么消息(脚本)文件名和行号将作为参数传递,这些都显示在弹出的错误对话框中。请注意,默认情况下,大多数用户都会关闭脚本错误对话框,所以如果目标用途是针对大量用户,这样做是明智的。

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.

我不确定如果有一个更简单的方法,我只使用旧的COM WebBrowser组件。

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

这篇关于在Webbrowser控件中捕获Javascript警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆