在 Web 浏览器控件中捕获 Javascript 警报
使用 webbrowswer 控件浏览站点。有时会出现涉及 javascript 弹出框的错误。当这种情况发生时,我想做一些事情。
- 知道何时弹出 JavaScript 警报。
我使用 LostFocus 事件取得了一些成功,但每当它失去焦点时,代码就会运行,这很烦人。
- 我想知道警报框显示的确切文字。
我不确定在哪里可以找到警报框对象或将其转换为在 C# 中使用。我查遍了整个互联网,但找不到它。
有人有任何线索吗?
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.
- 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.
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想捕获出现的脚本错误对话框,我建议捕获 window.onerror DOM 事件。如果为此事件分配一个处理程序,消息、(脚本)文件名和行号将作为参数传递,这些都是弹出的错误对话框中显示的内容。请注意,大多数用户默认情况下都会关闭脚本错误对话框,因此如果预期目的是针对大量受众,那么明智的做法是尊重这一点。
我不确定是否有更简单的方法,我只使用过旧的 COM WebBrowser 组件。
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.
只需这样做:
这将允许您对警报文本执行回调或任何您想要的操作。
Just do this:
This will allow you to do a callback or anything else you want with the alert text.